加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-3 – 使用Magical Unicorn的ASP.NET MVC自定义错误

发布时间:2020-12-16 03:22:15 所属栏目:asp.Net 来源:网络整理
导读:我的问题是关于Pure.Kromes回答 this post.我尝试使用他的方法实现我的页面的自定义错误消息,但有些问题我无法解释. 一个) 当我通过输入无效URL(例如localhost:3001 / NonexistantPage)引发404错误时,它默认为我的错误控制器的ServerError()Action,即使它应
我的问题是关于Pure.Kromes回答 this post.我尝试使用他的方法实现我的页面的自定义错误消息,但有些问题我无法解释.

一个)
当我通过输入无效URL(例如localhost:3001 / NonexistantPage)引发404错误时,它默认为我的错误控制器的ServerError()Action,即使它应该转到NotFound().这是我的ErrorController:

    public class ErrorController : Controller
    {
        public ActionResult NotFound()
        {
            Response.TrySkipIisCustomErrors = true;
            Response.StatusCode = (int)HttpStatusCode.NotFound;
            var viewModel = new ErrorViewModel()
            {
                ServerException = Server.GetLastError(),HTTPStatusCode = Response.StatusCode
            };
            return View(viewModel);
        }

        public ActionResult ServerError()
        {
            Response.TrySkipIisCustomErrors = true;
            Response.StatusCode = (int)HttpStatusCode.InternalServerError;

            var viewModel = new ErrorViewModel()
            {
                ServerException = Server.GetLastError(),HTTPStatusCode = Response.StatusCode
            };
            return View(viewModel);
        }
    }

我在Global.asax.cs中的错误路由:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("{*favicon}",new { favicon = @"(.*/)?favicon.ico(/.*)?" });

        routes.MapRoute(
            name: "Error - 404",url: "NotFound",defaults: new { controller = "Error",action = "NotFound" }
            );

        routes.MapRoute(
            name: "Error - 500",url: "ServerError",action = "ServerError" }
            );

我的web.config设置:

<system.web>
    <customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="/ServerError">
    <error statusCode="404" redirect="/NotFound" />
</customErrors>
...
</system.web>

<system.webServer>
    <httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="404" subStatusCode="-1" />
      <error statusCode="404" path="/NotFound" responseMode="ExecuteURL" />
      <remove statusCode="500" subStatusCode="-1" />
      <error statusCode="500" path="/ServerError" responseMode="ExecuteURL" />
</httpErrors>
...

Error视图位于/ Views / Error /中,作为NotFound.cshtml和ServerError.cshtml.

b)
一个有趣的事情是,当发生服务器错误时,它实际上显示我定义的服务器错误视图,但它也输出一个默认错误消息,说明找不到错误页面.

Here’s how it looks like:

你对我如何解决这两个问题有什么建议吗?我真的很喜欢Pure.Kromes实现这些错误消息的方法,但如果有更好的方法来实现这一点,请不要犹豫告诉我.

谢谢!

**编辑:**
通过访问/ Error / NotFound或Error / ServerError,我可以通过ErrorController直接导航到我的视图.

视图本身只包含一些文本,没有标记或任何东西.

正如我所说,它实际上在某种程度上起作用,而不是我想要它的工作方式. web.config中的重定向似乎存在问题,但我无法弄明白.

解决方法

当你有更复杂的路线并且有几个段时,这个设置还有一个问题.

http://localhost:2902/dsad/dadasdmasda/ddadad/dadads/ddadad/dadadadad/

我收到了服务器错误 – >

Sorry,an error occurred while processing your request.


Exception: An error occured while trying to Render the custom error view which you provided,for this HttpStatusCode. ViewPath: ~/Views/Error/NotFound.cshtml; Message: The RouteData must contain an item named 'controller' with a non-empty string value.
Source:

我的解决方案是在默认路由后最后添加其他路由

routes.MapRoute(
            "Default Catch all 404","{controller}/{action}/{*catchall}",new { controller = "Error",action = "NotFound" }
        );

希望它可以帮助某人:-)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读