.NET MVC2 How to change incoming URL parameters

I'm using .NET MVC 2 If an incoming request contains the URL:


where 1234 is an {id} parameter. I want to be able to use the id to get some data from a database and then change the URL so that it goes to a valid controller and action.

The new URL should look something like:


where the original id (1234) is looked up in the database and the data would translate to a specific {username}/{controller}/{action}/{id}.

I have the following routes set up in an AreaRegistration class:

    new { action = "Index", controller = "Home" },
    new string[] { "MyApp.Areas.Controllers" }

    new { action = "Index", controller = "Home" },
    new string[] { "MyApp.Areas.Controllers" }

What I can't seem to figure out is how/where to lookup the database data and change/rewrite the URL. I have tried implementing a custom RouteHandler and RouteBase but neither seem to do what I need.

This is my first SO post so forgive me if my question isn't clear. Any suggestions are appreciated.


You need to return a RedirectToAction() to perform the URL rewrite...

return this.RedirectToAction(action, controller);

There are a whole host of overloads for specifying ids, route values, etc...

As to looking it up in the database, that will depend on your data access model. Assuming Entity Framework or Linq it would be something like:

DataClasses1DataContext dc = new DataClasses1DataContext();

var record = from a in dc.GetTable<Order>() select id, username;

To clarify how MVC works...

I takes a URL like http://example.com/controller/action/id or http://example.com/area/controller/action/id

and calls the appropriate action method on the specified controller. Normally you would return a view however, you can send a lot of special data types back for different results eg JSON Data, HTTP Redirects, etc.

If an area/controller/action is omitted in the URL, the defaults from the route are used.


If you just want to show the appropriate page, you can leave the URL as http://example.com/1234 as long as the default action/controller has the code to display the appropriate view.

If you want to change the URL for aesthetic reasons, you would have the default controller/action take in an Id and return a RedirectToAction which points at the Controller/Action for the URL you want.

It's worth noting that it will generate the most minimal URL possible so if you were to have 2 actions on your default controller:

HomeController -> Index(int id)
HomeController -> ShowDetails(int id)

the URL for index would be something like


redirecting to ShowDetails would give a URL like this:


If ShowDetails were on a different (non-default) controller you'd get something like this:


That's assuming the routes follow the standard /Controller/Action/Id format. Needless to say by registering different routes, it swaps the parameters around as appropriate.

Hope that helps?

Need Your Help

Is it safe to allocate memory for buffers on external dll and use it on main application?

c++ c dll malloc

As in topic.. I've found something like this in one application. In main C application we have declaration:

Submitting multi-stage form using .load method


I've been looking at this multi-stage form at http://jsfiddle.net/xSkgH/89/ and was just wondering about the best way of submitting the form using the .load method.