简体   繁体   中英

How to do long static urls in asp.net mvc

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

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.

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM