【发布时间】:2014-10-26 17:38:30
【问题描述】:
我正在尝试在 ASP.NET MVC 5 中实现自定义错误页面。如果我输入了无效的 URL,自定义错误页面将在大多数情况下显示我的自定义 404 未找到页面,并且正确响应“404 未找到” ,但在某些情况下,它会以我的自定义 500 内部服务器错误页面响应“500 内部服务器错误”??
比如我有如下路由配置:
routes.MapRoute(
null,
"Category/{category}",
new { controller = "Post", action = "Category" }
);
现在如果我尝试导航到以下不存在的页面,它会响应 404 not found 错误,这是正确的。
justatestsite.com/Category/noexists(遵循正确的路由配置,页面不存在,更正错误状态404)
justatestsite.com/Category/noexists/norouteconfig(不遵循正确的路由配置,页面不存在,更正错误状态404)
但是使用我的其他路由配置:
routes.MapRoute(
null,
"Archive/{year}/{month}/{day}/{title}",
new { controller = "Post", action = "Detail" }
);
justatestsite.com/archive/2014/8/25/noexists(遵循正确的路由配置,页面不存在,错误状态 500)
justatestsite.com/archive/2014/8/25/noexists/norouteconfig(不遵循正确的路由配置,页面不存在,更正错误状态404)
这是我的帖子详细信息控制器
public ViewResult Detail(int year, int month, int day, string title)
{
var post = repository.Detail(year, month, day, title);
if(post == null)
{
throw new HttpException(404, "Post not found");
}
if (post.Published == false && User.Identity.IsAuthenticated == false)
{
throw new HttpException(401, "The post is not published");
}
return View(post);
}
为什么有时会响应错误状态码 500?????
【问题讨论】:
标签: asp.net-mvc routes http-status-code-404 custom-error-pages