ASP.Net MVC RC1 RouteCollection.MapRoute Problem - asp.net

I have a problem with the RC1 version of ASP.Net MVC. Whenever I add a Route before the "Default" route, the resulting Urls created are for the first Route added.
Here is my Routing in Global.asax.cs
routes.MapRoute(
"product-detailed",
"Products/{controller}/{action}/{id}",
new { controller = "ProductSubType", action = "Index", id = "" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
My Url creation:
<%= Html.ActionLink("Bikes", "Index", "Bikes") %><br />
<%= Html.RouteLink("Bikes", "product-detailed", new { controller = "Bikes", action = "Index" }) %>
I would expect the first ActionLink to create a Url like "/Bikes/Index" and the second RouteLink to create "/Products/Bikes/Index", but both Urls end up as "/Products/Bikes/Index".
What am I missing here on the routing?
Thanks.

You're not missing anything. It's working as designed.
Since the controller and action are both variable in the top route, with no limitations on valid values, then that route is valid for all values of controller and action.
Potential work-arounds:
Fix the controller and/or action values so that they're not part of the URL
Add restrictions for the top route for values of controller and/or action
Always use route links instead of action links, since they unambiguously state which route is the correct route.

Related

Submit form on my asp.net mvc view and the url does not show the controller and action.....How can those be shown?

Here is my form or an example of my form. :
#using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "indexForm" }))
{
<div>etc</div>
}
I then have a javascript function that will submit this form at distinct times. :
$("#indexForm").submit();
The problem is that when I go to my application initially the url is this. :
http://myWebsite/myApplication/myController/myAction
After the form is submitted via javascript here is my url. :
http://myWebsite/myApplication/
Due to the routing that I have it navigates to my index action without issue, but I really want that url to contain my controller and action.
Is this possible after the form is submitted?
The URL is myWebsite/myApplication/ after the form is submitted because you likely have the default route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
When a URL is generated in MVC the routes are used to construct the URL based on the values provided. If the values provided match the default values in the route than they will not appear in the URL because they are unnecessary. If you want the values to show in the route than remove them from the defaults. However, doing so will mean that a request to myWebsite/myApplication/ will no longer find your Home controller and Index action.

ASP.Net MVC No routes but default are working

I have added a new Controller to my application, and then from there added in a new View (right click, add view on the context menu)
I have added the route to the Route table, but when I browse to it I get a 404.
I can only assue that this is something fundamental that is wrong, as if I replace the values in the default route, I still cannot access my page.
The route table is;
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Classroom",
"{controller}/{action}/",
new { controller = "Classroom", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
//defaults: new { controller = "Classroom", action = "Index", id = UrlParameter.Optional }
);
there is only a return View(); in the index method. I have installed Glimpse, but as I am getting a 404, I cannot actually see the url that is being passed back.
So, what techniques / tricks are there I can use to track this down, (and any common gotcha's that could be causing this?)
Visual Studio 2012, using the inbuilt webserver.
This is Fixed. It was a stupid Gotcha on my part. The Controller was Called Classroom not ClassroomController
I have added the route to the Route table
No, you don't need to add any route for that. Get rid of the Classroom route from your Global.asax and leave only the Default one. Now you could safely navigate to your controller using /classroom/index which will render the Index action of the ClassroomController.

MVC 3 Route - Add old url parameter to new one automatically

I have two MapRoute in global asax.`
routes.MapRoute(
"AutoGeneratedURLHandler", // Route name
"{modulepath}/{controller}/{action}/{id}", // URL with parameters
new { id = UrlParameter.Optional } // Parameter defaults
);
//default route
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Account", action = "Logon", id = UrlParameter.Optional } // Parameter defaults
);`
And I have two link on masterpage.
<li><%:Html.ActionLink("Card Types", "Index", "CardType", new { modulepath="CardManagement" },null)%></li>
<li><%:Html.ActionLink("Home Page", "Index", "Home")%></li>
After I click Card Types link, Home Page link becomes "/CardManagement/Home/Index" instead of "/Home/Index".
Route values remember "modulepath" and add it auto to home page link. But I dont want this. How can I make it not add module path to url?
Instead of ActionLink, you could use a RouteLink and send it down your default route. Another option is to not use the ActionLink helper and just roll the link on your own:
Home

Hardcode URL in ASP.Net MVC 2 route

This is the default route that is given when you create a project -
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
But, here, only when you type http://example.com/Home/About the about page will be shown.
I want to show the about page when the user types http://example.com/About
How can I do it without writing a controller called About?
This is not working:
routes.MapRoute("About", "About", new { controller = "Home", action = "About", id = UrlParameter.Optional });
How can I modify it so that when /About is requested, about page is shown?
Try adding this Before your other route:
routes.MapRoute(
"Home", // Route name
"{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Keep in mind that "{controller}/{action}/{id}" will try to match the non-domain portion of your url. Therefore you need to try and match the url you want with a route.
You have the following url:
www.mydomain.com/About
Where you know the controller must be "Home" and the action must be "About". Therefore, you can match your route using something like this:
www.mydomain.com/{action}
However, the domain portion will need to be removed, so you end up with
{action}
You will then need to set the default route values as seen in the example above. Now, if we go to the domain, we will get routed to the "Home" controller and the "About" action. If we go to www.mydomain.com/HelloWorld, we would get routed to the "Home" controller and the "HelloWorld" action. we can add an optional "id" parameter like so:
{action}/{id}
But we'll need to make sure that the default id is set to UrlParameter.Optional
Hope this helps! :)
edit:
If you want to HARD CODE a url to a specifc route, you can set the default route parameters to the route you need, and then just use the non-domain portion of the url as the route capture.
so, instead of "{controller}/{action}/{id}", you would use "OldSite/MyOldPage.aspx"

Urlredirect in MVC2

In global.asax
routes.MapRoute(
"Test_Default", // Route name
"test/{controller}/{action}", // URL with parameters
new { }
);
routes.MapRoute(
"Default",
"{universe}",
new { controller = "notfound", action = "error"}
);
I have a controller: Home, containing an action: Index
Enter the url in browser: h**p://localhost:53235/test/home/index
Inside the index.aspx view in <body> tag: I want to link to the second route.
<%=Html.RouteLink("Link", new { universe = "MyUniverse" })%>
Shouldn't this generate a link to the second route in Global.asax? The generated url from the above is: h**p://localhost:53235/test/home/index?universe=MyUniverse. I can only get it to work, if I specify the name of the route: <%=Html.RouteLink("Link", "default", new { universe = "MyUniverse" })%>
Am I missing something?
As you've discovered you need to use the route name if you want to generate a link to the second route. The first route will always be evaluated because even if the universe parameter doesn't exist in the route definition it is just passed as query string argument.

Resources