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.

1
  • -- 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(); Commented Sep 20, 2011 at 21:23

2 Answers 2

2

Try to use Url.RouteUrl instead

window.location.href = '<%= Url.RouteUrl("Default", new { @Controller = "Feature", @Action = "Index"}) %>/' + $("#ProductId").val();
Sign up to request clarification or add additional context in comments.

7 Comments

the problem with double ids in this case-- localhost:61000/Feature.mvc/Index/… when I try to search the same id again
it seems to have truncated the url. here it is again Feature.mvc/Index/9a1347dc-60b0-4b3b-9570-9ed100b6bc6a/9a1347dc-60b0-4b3b-9570-9ed100b6bc6a
can you check the value from $("#ProductId").val()? The Url.RouteUrl part shouldn't generate the link with id.
it does. It is I think a feature of ASP.NET MVC routing. Look at stackoverflow.com/questions/5734679/… stackoverflow.com/questions/2088605/…
can you post me what '<%= Url.RouteUrl("Default", new { Controller = "Index", Action = "Feature"}) %>/' renders?
|
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.

4 Comments

@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"
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- 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
@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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.