Ambient values in mvc2.net routing

前端 未结 6 1927
生来不讨喜
生来不讨喜 2020-12-16 06:41

I have following two routes registered in my global.asax file

routes.MapRoute(
    \"strict\",
    \"{controller}.mvc/{docid}/{action}/{id}\",
          


        
6条回答
  •  清歌不尽
    2020-12-16 07:22

    My routing is defined this way:

    routes.MapRoute(
        "Planning",
        "Plans/{plan}/{controller}/{action}/{identifier}",
        new { controller = "General", action = "Planning", identifier = UrlParameter.Optional },
        new { plan = @"^\d+$" }
    );
    
    // default application route
    routes.MapRoute(
        "Default",
        "{controller}/{action}/{identifier}",
        new {
            controller = "General",
            action = "Summary",
            identifier = UrlParameter.Optional,
            plan = string.Empty // mind this default !!!
        }
    );
    

    This is very similar to what you're using. But mind my default route where I define defaults. Even though my default route doesn't define plan route value I still set it to string.Empty. So whenever I use Html.ActionLink() or Url.Action() and I want plan to be removed from the URL I call it the usual way:

    Url.Action("Action", "Controller", new { plan = string.Empty });
    

    And plan is not included in the URL query string any more. Try it out yourself it may work as well.

提交回复
热议问题