views:

40

answers:

1

I've got a unique requirement in my unique ASP.Net MVC project. Basically we are migrating an older Linux based website to MVC and we want to preserve the URLs which were on the last website.

It is not practical to create a new controller for the subdirectory (ex. 'www.mywebsite.com/pickes/cherry-pickle-recipe.html') of the website.

So I want to do one of the following

  1. Create a lookup list for the URLs. The URLs should be checked against a database and if a URL is found certain action should be returned from a certain controller.

  2. Trap all url requests which didn't have a controller and send them to a certain controller-> action.

How should I go about this?

+4  A: 

Well I think you can do this by writing a custom routehandler by implementing IRouteHandlerInterface

public class LookupRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        IRouteHandler handler = new MvcRouteHandler();
        var vals = requestContext.RouteData.Values;
        if(String.IsNullOrEmpty(vals["controller"])
        {
           // fetch action and controller from database
           vals["controller"] = dbcontroller;
           vals["action"] = dbaction;
        } 
        return handler.GetHttpHandler(requestContext);
    }
}

You have to do one more thing i.e register your route handler in global.asax like

routes.MapRoute(
   "dbroute",
   "{controller}/{action}/{id}",
   new {  id = "" }
   ).RouteHandler = new LookupRouteHandler();
Muhammad Adeel Zahid
Its worth saying - in your global.asax.cs on app startup, hit the db for the lookup table once, and cache that result somewhere, so you can use it here.
Nik
yes that will save db hit on every request +1
Muhammad Adeel Zahid