asp.net-mvc-3 – 使用Magical Unicorn的ASP.NET MVC自定义错误
我的问题是关于Pure.Kromes回答
this post.我尝试使用他的方法实现我的页面的自定义错误消息,但有些问题我无法解释.
一个) 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实现这些错误消息的方法,但如果有更好的方法来实现这一点,请不要犹豫告诉我. 谢谢! **编辑:** 视图本身只包含一些文本,没有标记或任何东西. 正如我所说,它实际上在某种程度上起作用,而不是我想要它的工作方式. 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" } ); 希望它可以帮助某人:-) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – 用于枚举的IRouteConstraint
- ASP报表分页设置
- 为什么在ASP.NET中刷新页面时会执行按钮单击事件?
- asp.net-mvc-4 – ASP .NET MVC 4包含两个部分视图的视图,每
- asp.net-mvc – 我有15分钟向我的同事介绍ASP.NET MVC 我应
- 领先技术 - ASP.NET Core 中的 Cookie、声明和身份验证
- asp.net – 什么是.MSPX文件扩展名?
- asp.net – Request.IsLocal是安全还是可以欺骗?
- asp.net – 默认的Razor cshtml名称空间定义在哪里?
- asp.net – 在Global.asax上使用IHttpModule