Custom Error Page for ASP.Net MVC

An error happens, and when it happens you want to display something nice to users, here is how.

Go to web.config, find <system.web>

Now go to App_Start/FilterConfig.cs, make sure filter is added, as below;

Then go to Views/_Shared, create new view.  Example;

 

ASP.Net Return Empty Results

When you get request but not able to return appropriate result back, it is good idea to return http status code to let them know what’s wrong.

https://msdn.microsoft.com/en-us/library/system.web.mvc.httpstatuscoderesult(v=vs.118).aspx

And here is the list full

https://msdn.microsoft.com/en-us/library/system.net.httpstatuscode(v=vs.110).aspx

Ones that I use often

When input parameter is not valid
HttpStatusCodeResult(HttpStatusCode.BadRequest)

When the access is unauthorized
HttpStatusCodeResult(HttpStatusCode.Unauthorized)

When there is nothing to show (likely had no matching data record to create page)
HttpStatusCodeResult(HttpStatusCode.NotFound)

And when you want to tell search engine that page has been moved permanently, then use