ASP.NET MVC 里redirectMode="ResponseRewrite" 时候无法使用 Controller 来设置特定的错误页面。
我做了如下设置:
(1)web.config:
<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="Error/defaulterror">
<error statusCode="404" redirect="Error/404"/>
</customErrors>
(2)ErrorController.cs:
public ActionResult Error(string ErrorCode)
{
ViewData["ErrorCOde"] = ErrorCode;
return View();
}
出现下列错误:
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: /
查询了资料发现ResponseRewrite的时候系统使用的是Server.Transfer的方式来显示错误信息的。而Server.Transfer与asp.net mv 路由是不兼容的。所以如果使用“ResponseRewrite”的时候defaultRedirect或者 Error tag里的redirect 必须是实际存在的页面才可以。如下面的配置:
<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Common/Error/DefaultError.aspx">
<error statusCode="404" redirect="~/Common/Error/404.aspx"/>
</customErrors>
It is important to note for anyone trying to do this in an MVC application that ResponseRewrite uses Server.Transfer behind the scenes. Therefore, the defaultRedirect must correspond to a legitimate file on the file system. Apparently, Server.Transfer is not compatible with MVC routes, therefore, if your error page is served by a controller action, Server.Transfer is going to look for /Error/Whatever, not find it on the file system, and return a generic 404 error page!
http://www.chinapoesy.com
诗词在线 |唐诗|宋词|元曲|现代诗歌|外国诗歌
126在线阅读网
http://www.Read126.cn
126在线阅读网 人物传记、古典名著、历史书籍。。。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2006-12-28 Sql Server 行转列学习 根据学生表、课程表、学生成绩表统计每个学生的各科成绩和他的总成绩、平均成绩