views:

57

answers:

2

Hi, what would be the best way to create long static urls in asp.net MVC? For example say I wanted to create the following url.

Ex:

/Packages/Somepackage/package a

+1  A: 

Create some standard routes definitions, like these:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}"
); 

routes.MapRoute(
    "Default2",
    "{controller}/{action}/{id}/{id2}"
);

Then create a controller names Packages and some actions in it like this:

Define the action like this:

public ActionResult Somepackage(string id) 
{
    if (!string.IsNullOrEmpty(id) && id == "package")
    {
        //do something
    }
}

public ActionResult Somepackage2(string id, string id2) 
{
    if (!string.IsNullOrEmpty(id) && id == "package" 
    && !string.IsNullOrEmpty(id2) && id2 == "package2")
    {
        //do something else
    }
}
Josh Pearce
Thanks for the detailed answer. So to create longer static pages I would need to create additional controller and routes for them?
chobo
yeah, pretty much, but as Robert says, you can add extra stuff in there which is ignored. You can make routes do anything you like, but it requires some reading up to understand how they work.
Josh Pearce
+2  A: 

If you are doing this for search engine optimization, there might be a better way.

Examining the URL for this question, you will see that there is a controller called questions, an id so that the question can be looked up from the database, and a long static part of the URL for SEO:

http://stackoverflow.com/questions/1786793/how-to-do-long-static-urls-in-asp...

The long static part of the URL is just a parameter in the controller method, stored as a text field in the database. You can put anything in that field that you want, as long as you separate the words with dashes (which is what the search engines seem to prefer anyway). It is an optional one too, so that links like

http://stackoverflow.com/questions/1786793

...also work.

Robert Harvey