1
votes

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?????

3

3 Answers

2
votes

Figured out what was going wrong. I thought it has something to do with routing configuration, but the problem was actually in my LINQ expression.

I used

.Single()

to return a single post, so there must be 1 post returned and not 0. When I go to the url with no post in it, there is 0 post, so it throws me internal server error instead of not found error.

I changed it to

.SingleOrDefault()

so the expected value can either be single(1) or default(0), now it is throwing the 404 not found error, which is what I want.

1
votes

You may check this blog to implement custom error pages in your ASP.NET MVC app. Here you will be able to handle both 404 not found and 500 internal server errors as well.

http://blog.janjonas.net/2011-12-11/asp-net-mvc3-custom-error-pages-non-ajax-requests-jquery-ajax-requests

Hope this helps :)

1
votes

You're getting a 500 internal server error because there's a problem within your Detail action on the PostController what is the actual error message you're getting, and what does your action look like?

How are you handling the fact that you can't find the post in question? I'd guess that the you're getting nothing back for the posting, and then falling over.

Have you tried something like:

public ActionResult Post(int year, int month, int day, string path)
{
  var publishedDate = new DateTime(year, month, day);

  BlogPost blog = _BlogRepository.GetBlogPost(publishedDate, path);

  if (null == blog)
  {
    throw new HttpException(404, "Blog post not found");
  }

  return View(blog);
}