I'm trying to have 2 custom error pages in a stock sample ASP.NET MVC3 website.
Darin Dimitrov has a good SO answer here but it's not working for all my test conditions.
Then there is the widely popular How can I properly handle 404s in ASP.NET MVC? post .. but this just touches on a 404 error.
Can someone please explain what
- Routes
web.config
settings
- controllers / action methods
are required to do this very simple thing :(
Scenarios to accept this answer:
- VS2010 -> File -> New -> ASP.NET MVC3 project / Internet Application
- (Right Click on the solution).. -> Use IIS Express
- Error pages cannot be a static html page. They must be a page which I can pass info to, like any other view, etc. (eg. an
ErrorController
)...
and for the test routes...
/Home/Index
-> shows index page
/Home
-> shows index page
/
-> shows index page
/Home/About
-> shows about page
/asdasd/asdsad/asdas/asddasd/adsad
-> 404
/adsa/asda/asd/asd/asd/asd
-> 404
/asdsadasda
-> 404
then add this to the HomeController.cs
class..
public ActionResult ThrowException()
{
throw new NotImplementedException();
}
and now ..
/home/throwexception
-> 500 error
Cheers :)
BTW, some of those realy long routes (above) give really weird error pages right now, when using the stock standard new ASP.NET MVC3
template
I specify customErrors in my web.config file as below;
<customErrors defaultRedirect="/Error/GeneralError" mode="On">
<error statusCode="403" redirect="/Error/403" />
<error statusCode="404" redirect="/Error/404" />
<error statusCode="500" redirect="/Error/500" />
</customErrors
I have a route in the Global.asax as below;
/// Error Pages
///
routes.MapRoute(
"Error", // Route name
"Error/{errorCode}", // URL with parameters
new { controller = "Page", action = "Error", errorCode= UrlParameter.Optional }
);
The corresponding ActionResult does the following, which returns the relevant custom error page.
//
public ActionResult Error(string errorCode)
{
var viewModel = new PageViewModel();
int code = 0;
int.TryParse(errorCode, out code);
switch (code)
{
case 403:
viewModel.HtmlTitleTag = GlobalFunctions.FormatTitleTag("403 Forbidden");
return View("403", viewModel);
case 404:
viewModel.HtmlTitleTag = GlobalFunctions.FormatTitleTag("404 Page Not Found");
return View("404", viewModel);
case 500:
viewModel.HtmlTitleTag = GlobalFunctions.FormatTitleTag("500 Internal Server Error");
return View("500", viewModel);
default:
viewModel.HtmlTitleTag = GlobalFunctions.FormatTitleTag("Embarrassing Error");
return View("GeneralError", viewModel);
}
}
This allows me to have many different custom error pages. It's maybe not the best or most elegant solution, but it certainly works for me.
You can set your error page information in the web.config. This page talks about doing this and using the HandleErrorAttribute set on your controllers so they use this configuration.
http://deanhume.com/Home/BlogPost/custom-error-pages-in-mvc/4
This seems to be a big question here.
See my answer is this post:
Once and for all what is the best routing approach to handle errors, exceptions and 404's in MVC
But i would add these routes to your test cases:
http://www.website.com/yourcontroller/youraction:32
and
http://www.website.com/yourcontroller/youraction/http:/www.website.com/yourcontroller/youraction?par=123