ASP.NET MVC - Mapping more than one query string parameter to a pretty url

送分小仙女□ 提交于 2019-12-21 04:25:12

问题


I am a bit stuck on the design of my seo friendly urls for mvc....Take for example the following url: http://myapp/venues/resturants.aspx?location=central&orderBy=top-rated

With my mvc app i have mapped it as follows: http://myapp/venues/list/resturants/central/top-rated
{controller}/{action}/{category}/{location}/{order}

Now the only problem is that location and order are optional...so it should be possible to submit a request like: http://myapp/venues/list/resturants/top-rated . This proves to be a problem when the request hits the controller action, the location parameter has picked up "top-rated", naturally.

Any suggestions? I' am considering using explicit querystrings to handle more than one parameter but this is really my last option as i dont want to sacrifice SEO too much.

Has anyone eles run into such dilemmas? And how did you handle it?

Thanks in advance!


回答1:


Assuming that the allowed values for location and order are unique (i.e. when they come in, you can tell them apart, or else if they only supply one, how are you going to know if it's a location or an order?), then you could just take two parameters and work out what they are in the controller.

Route: {controller}/{action}/{param1}/{param2}

Controller action:

public ActionResult MyAction(string param1, string param2)
{
    string location;
    string order;
    if (!ParseLocation(param1, out location))
    { ParseLocation(param2, out location); }
    // ...
}

Not particularly elegant, but does let you have the URLs you want.




回答2:


Click on your profile link and look at the URLs for Stats, Recent, Response, etc.

Examples:

  • https://stackoverflow.com/users/52065?sort=recent#sort-top
  • https://stackoverflow.com/users/52065?sort=stats#sort-top

with no sort it defaults to stats

  • https://stackoverflow.com/users/52065

Optional paramters should be query parameters




回答3:


You will always have this issue if you have multiple optional parameters. Either make one or both of them non-optional (and positioned earlier in the query string than the optional one) or use the querystring parameter notation.




回答4:


ok guys just posting a solution i've been playing with so far.

I have set up my routes using constraints as follows:

        routes.MapRoute(
            "VenuesList",                                              
            "venues/list/{category}/{location}/{orderBy}",             
            new { controller = "venues", action = "list", category = "", location = "", orderBy = "" },
            new { location = "central|east|west|south", orderBy = "top-rated|price" }
        );

    routes.MapRoute(
            "VenuesListByLocation",                                              
            "venues/list/{category}/{location}",                           
            new { controller = "venues", action = "list", category = "", location = "" },
            new { location = "central|east|west|south" }
        );

        routes.MapRoute(
            "VenuesListByOrder",                                              
            "venues/list/{category}/{orderBy}",                           
            new { controller = "venues", action = "list", category = "", orderBy = "" },
            new { orderBy = "top-rated|price" }
        );

        routes.MapRoute(
            "VenuesListDefault",                                              
            "venues/list/{category}",                           
            new { controller = "venues", action = "list", category = "" }

        );

        routes.MapRoute(
            "Default",                                              
            "{controller}/{action}/{id}",                           
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        ); 

The idea is that if the validation fails it will go to the next route in the list...eventually hitting the default.

Needs some more testing but has worked well so far...




回答5:


Why don't you create a property in the page for each possible querystring parameter?

This way you can handle it any way you choose with just a few lines of code...



来源:https://stackoverflow.com/questions/417000/asp-net-mvc-mapping-more-than-one-query-string-parameter-to-a-pretty-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!