ASP.NET MVC Url.Action and route name value

徘徊边缘 提交于 2019-12-03 11:36:19

问题


I am using asp.net mvc 2 and create localization based on routes.

  1. my route looks like: {culture}/{controller}/{action}
  2. I go to my home controller: en/Home/Index
  3. my home controller view have a links to other controllers:

    <a href='<%= Url.Action("Prods","Products") %>' >Products</a>
    <a href='<%= Url.Action("Index","About") %>' >About</a>
    

First link generated code: /en/Products/Prods but second one generate: /Home/Index

I can't understand why Url.Action skips the {culture} route parameter when I pass value Index in argument action? What am I doing wrong?

Route configuration:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute("Login", // Route name
                "{controller}/Index", // URL with parameters
                new { controller = "Login", action = "Index" } // Parameter defaults
                ).RouteHandler = new SingleCultureMvcRouteHandler();

routes.MapRoute("Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
               );      

Then

foreach (Route r in routes)
{
    if (!(r.RouteHandler is SingleCultureMvcRouteHandler))
    {
       r.RouteHandler = new MultiCultureMvcRouteHandler();

       r.Url = "{culture}/" + r.Url;

       if (r.Defaults == null)
       {
          r.Defaults = new RouteValueDictionary();
       }

       r.Defaults.Add("culture", "en");

       if (r.Constraints == null)
       {
          r.Constraints = new RouteValueDictionary();
       }

       r.Constraints.Add("culture", new CultureConstraint(cultures));
    }
 }

Thanks for all help


回答1:


When generating URL your best options is to always use the route names because this way you do not get into the subtleties of the algorithm used to select the route that will be used to generate the URL.

My advice if for you not to use Url.Action and instead use Url.RouteUrl which allows you to specify the name of the route that should be used to construct the URL.

By always using route names you also make your code more robust to changes, for example, you can add new routes without having to worry that they might break your existing code for URL generation.




回答2:


For purposes of building a Url.Action link, any Index action without an id token will match the Login route. The Login route uses the SingleCultureMvcRouteHandler, so the culture will not be prepended to those links.



来源:https://stackoverflow.com/questions/7938332/asp-net-mvc-url-action-and-route-name-value

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