How to prevent Url.RouteUrl(…) from inheriting route values from the current request

前端 未结 2 1768
孤城傲影
孤城傲影 2020-12-29 02:46

Lets say you have an action method to display products in a shopping cart

 // ProductsController.cs
 public ActionMethod Index(string gender) {

      // get         


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-29 03:43

    Maybe I'm missing something here, but why not set it up like this:

    In your global asax, create two routes:

    routes.MapRoute(
        "testimonial-mf",
        "Testimonials/{gender}",
        new { controller = "Testimonials", action = "Index" }
    );
    
    routes.MapRoute(
        "testimonial-neutral",
        "Testimonials/Neutral",
        new { controller = "Testimonials", action = "Index", gender="Neutral" }
    );
    

    Then, use Url.Action instead of Url.RouteUrl:

    <%= Url.Action("Testimonials", "Index") %>
    <%= Url.Action("Testimonials", "Index", new { gender = "Male" }) %>
    <%= Url.Action("Testimonials", "Index", new { gender = "Female" }) %>
    

    Which, on my machine resolves to the following urls:

    /Testimonials/Neutral 
    /Testimonials/Male 
    /Testimonials/Female
    

    I'm not sure if this is an acceptable solution, but it's an alternative, no?

提交回复
热议问题