views:

145

answers:

3

Are there any good examples of mvc routing wherein every 404 page not found request is routed to a standard view in MVC which basically pulls the content from the database.

A: 

modify the web.config file, you may Reference to this page and look at the setting custom error pages in web.config section.

xandy
+1  A: 

Here's one way to do this: In your global.asax file in Application_Start, you need to set the default controller factory. Override it with an instance of your own factory.

void Application_Start()
{
    ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());
}

MyControllerFactory should inherit from DefaultControllerFactory and when selecting the controller to use, look in your database for the appropriate page you want to display. If the page exists, select the appropriate controller and override the action in the requestContext.RouteData collection to point at the appropriate action for displaying dynamic pages.

If the requested page doesn't exist, pass back a call to the base method and let it do what it would normally do.

There are other ways you could do it, but this one should work and allows you to intercept the request before you hit the 404 page.

Nathan Ridley
+1  A: 

Just add this route to the bottom of your RouteTable:

routes.MapRoute("DynamicPages", "{*page}", new { Controller = "DynamicPages", Action = "Show", Page = String.Empty });

And create a controller for displaying dynamic pages from db:

public class DynamicPagesController : Controller
{
    public ActionResult Show(string page)
    {
        var pageContent = DB.GetContentForPage(page);

        return Content(pageContent);
    }
}
eu-ge-ne