无法让 defaultRedirect 工作
在我的 web.config 中,我有:
<system.web>
<customErrors mode="On" defaultRedirect="Error.cshtml" />
</system.web>
在 Views/Shared/Error.cshtml 中,我有:
@model System.Web.Mvc.HandleErrorInfo
@{
ViewBag.Title = "Error";
}
<h2>
Sorry, an error occurred while processing your request.
</h2>
如果我将无效的 URL/路由放入浏览器中,我会得到以下结果:
“/”应用程序中的服务器错误。
找不到资源。
描述:HTTP 404。您正在查找的资源(或其依赖项之一)可能已被删除、名称已更改或暂时不可用。请检查以下 URL 并确保拼写正确。
请求的 URL:/Error.cshtml
为什么找不到Error.cshtml?该文件肯定位于“视图/共享”中。
In my web.config, I have:
<system.web>
<customErrors mode="On" defaultRedirect="Error.cshtml" />
</system.web>
In Views/Shared/Error.cshtml, I have:
@model System.Web.Mvc.HandleErrorInfo
@{
ViewBag.Title = "Error";
}
<h2>
Sorry, an error occurred while processing your request.
</h2>
If I put an invalid URL/route into my browser, I get this:
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /Error.cshtml
Why can't Error.cshtml be found? The file is definitely in Views/Shared.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
defaultRedirect 不会直接转到视图。您的 defaultRedirect 看起来像一个无法处理的剃刀视图文件。例如:它从哪里获取模型?它不是也不能在配置文件中指定,因此它无法处理视图。
如果您想要 MVC 中的更多动态错误页面,您可能需要阅读 MVC 3 中的自定义错误页面和错误处理
The defaultRedirect won't go directly to a view. Your defaultRedirect looks like a razor view file which it can't process. For example: Where does it get the model from? It isn't, and can't, be specified in the config file so it can't process a view.
If you want more dynamic error pages in MVC you might want to read custom error pages and error handling in MVC 3
这对我有用。只需在 web.config 中进行以下更改:
It works for me. Just make this change in web.config:
将 web.config 编辑为:
如果您使用 asp.net mvc 1.0 或 2.0,则应该为每个控制器添加 HandleError 属性:
如果您使用 asp.net mvc 3.0:
Edit you web.config as:
If you use asp.net mvc 1.0 or 2.0, you should add HandleError attribute for each controller:
if you use asp.net mvc 3.0:
您应该使用
[HandleError]
属性标记您的控制器操作,或将其注册为全局过滤器编辑:
发生这种情况是因为自定义错误的重定向功能属于 ASP.NET 基础结构,并且对 ASP.NET MVC 一无所知。
您应该创建单独的控制器来管理重定向并在 Web.config 中指定它的 url。或者您可以创建静态 html 文件并将其放入根目录(或除 Views 目录之外的其他位置)
You should mark your controller action with
[HandleError]
attribute or register it as a global filterEDIT:
It happens because of custom errors' redirects functionality belongs to ASP.NET infrastructure and knows nothing about ASP.NET MVC.
You should create separate controller for managing redirects and specify url to it in your Web.config. Or you could create static html file and put it into root directory (or wherever else except Views directory)
一种解决方案是创建 ErrorController
更改 Web.config
One solution is create ErrorController
Change Web.config