ASP.NET MVC Routing and URL.Action

asp.net-mvc-2asp.net-mvc-routing

in one of my asp.net mvc 2 views I am have the following statement

window.location.href = '<% = Url.Action("Index","Feature", new {id=""}) %>/' + $("#ProductId").val();

as can be seen $("#ProductId").val() can only be computed from client action and so outside url.Action

I have my Routes as shown below:

 public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("{*allaspx}", new { allaspx = @".*\.aspx(/.*)?" });
            routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });
            routes.IgnoreRoute("{*allimages}", new {allimages = @".*\.jpg(/.*)?"});
           routes.MapRoute(
              "Default", // Route name
              "{controller}.mvc/{action}/{id}", // URL with parameters
              new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
          );


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


        }

The request with Url.Action fails because the route thinks the "action" is the "id"
enter image description here

How can I make sure the route configuration in "DefaultIndex" is validated and the url is

Key >> Value
controller = Feature
action = index
id = 9a1347dc-60b0-4b3b-9570-9ed100b6bc6a

Edit 2

Image 2:

enter image description here

Edit 1- Route Order

I almost thought I solved it by changing the route order

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



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



            routes.MapRoute("Root", "", new { controller = "Home", action = "Index", id = "" });

it did work for
http://localhost:61000/Feature.mvc/9a1347dc-60b0-4b3b-9570-9ed100b6bc6a/

but failed for a post on same page

http://localhost:61000/Product.mvc/List

**** History ****

Have been using + $("#ProductId").val();
="/domainname[virtual directory alias]/Fewature.mvc/Index"+$("#ProductId").val();

always worked, though I had to change all scripts when posted to server domainname[virtual directory alias]/ changes from development to test to production

trying to streamline it:

Had the following statement earlier:

window.location.href = '<% = Url.Action("Index","Feature"}) %>/' + $("#ProductId").val();

Would result in multiple Id values

http://localhost:61000/Feature.mvc/9a1347dc-60b0-4b3b-9570-9ed100b6bc6a/3c5941e4-cb25-4d6f-9117-9ed100b4ca91

it maps existing route {id} into Url.Action("Index","Feature"}

resulting in NoMatch

Introduced

new {id=""} to get around it.

Best Answer

Try to use Url.RouteUrl instead

window.location.href = '<%= Url.RouteUrl("Default", new { @Controller = "Feature", @Action = "Index"}) %>/' + $("#ProductId").val();
Related Topic