I am trying to implement custom error pages in ASP.NET MVC 5. If I input a invalid URL, the custom error page will show my custom 404 not found page most of the times with the correct response "404 not found", but in some cases it responses with a my custom 500 internal server error page with the response "500 internal server error"??
For example, I have the following routing configuration:
routes.MapRoute(
null,
"Category/{category}",
new { controller = "Post", action = "Category" }
);
Now if I try to navigate to the following pages which doesn't exists, it will respond 404 not found error, which is correct.
justatestsite.com/Category/noexists (follows the correct routing configuration, page doesn't exists, correct error status 404)
justatestsite.com/Category/noexists/norouteconfig (doesn't follow the correct routing configuration, page doesn't exists, correct error status 404)
However with my other routing configuration:
routes.MapRoute(
null,
"Archive/{year}/{month}/{day}/{title}",
new { controller = "Post", action = "Detail" }
);
justatestsite.com/archive/2014/8/25/noexists (follows the correct routing configuation, page doesn't exists, incorrect error status 500)
justatestsite.com/archive/2014/8/25/noexists/norouteconfig (doesn't follow the correct routing configuration, page doesn't exists, correct error status 404)
Here is my post detail controller
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);
}
Why is it sometimes responding with error status code 500?????