ASP.NET MVC Routing and URL.Action

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" 在这里输入图像描述

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:

在这里输入图像描述

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.


尝试使用Url.RouteUrl来代替

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

Your routing is all messed up. What's the reason for the second route: {controller}.mvc/{id} ... ? It's clashing with the first route.

If a URL like /mycontroller/9a1347dc-60b0-4b3b-9570-9ed100b6bc6a comes in the routing engine will always route it to {controller}/{action}/{id} because it's first in the list and the url can be mapped to that route, ie there are no route constraints and id is optional.

If I was you, I would just remove the second route... if you really need the second route then move it above the first route and then put a route constraint on it.

链接地址: http://www.djcxy.com/p/57854.html

上一篇: MVC 3.0 Maproute编码

下一篇: ASP.NET MVC路由和URL.Action