4

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.

TheMar
  • 1,934
  • 2
  • 30
  • 51
  • -- Came up with a hack but I dont want to use it-- Idea is to introduce a "/" and then Url.Action renders action ("index")window.location.href = '<% = Url.Action("Index","Feature", new {id="/"}) %>' + $("#ProductId").val(); – TheMar Sep 20 '11 at 21:23

2 Answers2

2

Try to use Url.RouteUrl instead

window.location.href = '<%= Url.RouteUrl("Default", new { @Controller = "Feature", @Action = "Index"}) %>/' + $("#ProductId").val();
Henry Oh
  • 21
  • 4
  • the problem with double ids in this case-- http://localhost:61000/Feature.mvc/Index/9a1347dc-60b0-4b3b-9570-9ed100b6bc6a/9a1347dc-60b0-4b3b-9570-9ed100b6bc6a when I try to search the same id again – TheMar Sep 20 '11 at 17:08
  • it seems to have truncated the url. here it is again Feature.mvc/Index/9a1347dc-60b0-4b3b-9570-9ed100b6bc6a/9a1347dc-60b0-4b3b-9570-9ed100b6bc6a – TheMar Sep 20 '11 at 17:10
  • can you check the value from $("#ProductId").val()? The Url.RouteUrl part shouldn't generate the link with id. – Henry Oh Sep 20 '11 at 17:45
  • it does. It is I think a feature of ASP.NET MVC routing. Look at http://stackoverflow.com/questions/5734679/url-actionaction-controller-routevalues-doubling-up-id-in-url http://stackoverflow.com/questions/2088605/url-action-only-render-controller-and-action-but-not-id – TheMar Sep 20 '11 at 18:58
  • can you post me what '<%= Url.RouteUrl("Default", new { Controller = "Index", Action = "Feature"}) %>/' renders? – Henry Oh Sep 20 '11 at 21:12
  • Oops my bad... should be '<%= Url.RouteUrl("Default", new { Controller = "Feature", Action = "Index"}) %>/' – Henry Oh Sep 20 '11 at 21:14
  • I did correct it , forgot to mention it earlier. Check Edit 2 in my question above it shows rendering for both URL.Action and Url.RouteUrl – TheMar Sep 20 '11 at 21:21
1

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, i.e. 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.

Charlino
  • 15,802
  • 3
  • 58
  • 74
  • @Charlino- I tried moving the 2nd route to the top. It worked as I said in my edit 1 but somehow my JQGrids post started to fail. On the constraints , is it possible that if url is like /mycontroller/9a1347dc-60b0-4b3b-9570-9ed100b6bc6a and the last part conforms to Guid to have it mapped to {Id} and have action defaulted to "Index" – TheMar Sep 20 '11 at 17:20
  • Yes, that's possible. But you're making things hard for yourself... why don't you just stick with the one route `{controller}/{action}/{id}`? – Charlino Sep 20 '11 at 17:28
  • @Charlino- I would like to. but <% = Url.Action("Index","Feature", new {id=""}) %>/' + $("#ProductId").val(); evaluates to Feature.mvc/9a1347dc-60b0-4b3b-9570-9ed100b6bc6a >> Routing takes action =id – TheMar Sep 20 '11 at 19:03
  • @charlino- checkout Image 2 under Edit 2 in my question. If I make use of default routing, url.Action evaluates to Feature.mvc/Feature.mvc/9a1347dc-60b0-4b3b-9570-9ed100b6bc6a. if it had the index, the url will work. anyway to force Index to show up in Url.Action if {id} is empty or null – TheMar Sep 20 '11 at 19:35