我想在ASP.NET MVC 5中实现自定义错误页面。如果我输入无效的URL,自定义错误页面将显示我的自定义404大多数时候没有找到正确的响应“404未找到”的页面,但在某些情况下,它会响应我的自定义500内部服务器错误页面,响应为“500内部服务器错误”?为什么ASP.NET MVC有时会返回错误状态码500,有时错误状态码404
例如,我有以下的路由配置:
routes.MapRoute(
null,
"Category/{category}",
new { controller = "Post", action = "Category" }
);
现在,如果我尝试导航到不存在的页面,它会回应404未找到错误,这是正确的。
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(如下正确的路由configuation ,页面不'牛逼存在,不正确的错误状态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响应?????
啊,是的,旧的Single/First'OrDefault'栗子;) – 2014-09-03 12:55:51