Creating A new Route in MVC - asp.net-mvc

In MVC, the default route url pattern is - url : "{controller}/{action}/{id}"
When I add a new route as shown below before the default route, the url for the default route is shown as something like Home/Index?id=5 and not Home/Index/5. How can this be fixed.
routes.MapRoute(
name: "Name",
url: "{controller}/{action}/{name}",
defaults: new { controller = "Home", action = "Browse", name = UrlParameter.Optional }
);

The default route will never be hit because the route you added is exactly the same, from a routing perspective. So your route will catch everything that the default route would catch if it were the only one, or placed before yours. Both will match one, two and three-segment URLs.

That route is unnecessary and pretty much useless.
like what #asymptoticFault says, it serves the same purpose as the default one.

Related

How can I get T4MVC to work with a route like "{controller}/{action}/{id}"?

I have an ASP.NET MVC 5 website.
Following the suggestion in various blogs, I used T4MVC to change my routing from:
context.MapRoute(
name:"MyArea_default",
url:"MyArea/{controller}/{action}/{id}",
defaults:new { action = "Index", id = UrlParameter.Optional }
);
...to...
context.MapRoute(
name: "MyArea_default",
url: "MyArea/{controller}/{action}",
defaults: MVC.MyArea.Orders.Index()
);
This "worked", but I did wonder about the removal of the {id} segment from the URL pattern. And sure enough, the URLs have changed from (e.g.):
/MyArea/Orders/Edit/1
...to:
/MyArea/Orders/Edit?id=1
I'm really not a fan of the query string parameter, so I'd like to use the previous form.
I tried putting the {id} part back into the route definition:
context.MapRoute(
name: "MyArea_default",
url: "MyArea/{controller}/{action}/{id}",
defaults: MVC.MyArea.Orders.Index()
);
...but of course the defaults no longer provide a default value for id, and so that doesn't work. If the Index action took an optional id parameter, then I'd be able to do this:
context.MapRoute(
name: "MyArea_default",
url: "MyArea/{controller}/{action}/{id}",
defaults: MVC.MyArea.Orders.Index(null) // <- DEFAULT FOR ID
);
...but it doesn't, so I'm stuck.
Also, I'm nervous about the fact that a specific controller is now specified in the route definition. Prior to the T4MVC changes, the route did not specify a controller. Does the new style now mean that I need a route per-controller?
You should just look at T4MVC as a typing aid to avoid magic values. Beyond that, everything works the same way, so you're not any more tied to a specific controller than before.
To solve your issue, please try:
defaults: MVC.MyArea.Orders.Index().AddRouteValue("id", UrlParameter.Optional)

ASP.MVC 3 routing : how to get url with default action included?

Suppose I have the following routing
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } );
Now, when you generate an url using Url.Action("Index","MyController") you will get as expected : /MyController
But in one exceptional case, I would like to get the full url /MyController/Index
(without changing the routing)... does anyone know if this is possible?
It is possible. But you need to modify the routing.
Create an own routing class that derives Route
Override the GetVirtualPath() method to include /index for the pages that needs it.
Configure the default route using your routing class instead.
I am afraid this is not possible. And it shouldn't matter as both urls will resolve to the same controller action.

Can't Route An MVC Controller

I have tried routes.mapRoute but i can't figure a way in MVC 3 to use it make a root path route to an action. e.g mywebsite.com/party should redirect to mywebsite.com/events/party where events is the controller and party is the action.
Is this even possible?
Without seeing your existing routes, its hard to give you an exact solution.
One rule to keep in mind:
MVC will resolve the first in your route collection that matches the requested URL
not necessarily the most specific match.
Make sure you do not have another rule that would also satisfy that route placed earlier in your code, e.g. the routing algorithm might be finding a "party" controller and "index" action because you have a default rule like:
routes.MapRoute(
"Default",
"{action}",
new { controller = "Home", action = "Index" }
);
placed before your rule.
You need to put something like
routes.MapRoute(
"PartyRoute",
"party",
new { controller = "Events", action = "Party" }
);
BEFORE any route that might match a URL with just a single parameter
routes.MapRoute(
"Default",
"{action}",
new { controller = "Events", action = "Index" }
);

MVC Routing: Trying to get dynamic root value working

I am trying to define dynamic sections of my site with the root url of the site. I am having some trouble defining the right MVC Route for it. Can someone please help.
My desired url will look like this: http://website.com/[dynamic-string]
But I have other standard pages like: http://website.com/about or http://website.com/faq or even just http://website.com.
My routes don't work correctly with that dynamic string. As shown below.
This is the route for the dynamic-string.
routes.MapRoute(
"CommunityName", // Route name
"{communityName}", // URL with parameters
new { controller = "Community", action = "Community", communityName = UrlParameter.Optional }
);
This is the route for all other STANDARD PAGES
routes.MapRoute(
"Default", // Route name
"{action}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
My routes just don't match up. Everything either gets diverted to one or the other route depending on which route is declared first.
There is no difference between the two routes you mention. How can MVC know which url should be mapped to communityName and which to action? Any url can match both.
You can define your standard pages as a route (before the CommunityName route) or you can catch them in your Community action, see if the name matches a function in your Home controller and then call the right action function.
I've never done this before but you might be able to create a more intelligent routehandler that looks at your controller actions, checks if the action really exists and if true selects that route.
this is beacuse the routes are effectively the same. When you declare the action route you do not state any constraints to the route, for this reason anything will be assumed to be a the action name.
If you want two routes to capture at the same level then you must constrain the action names to those that exist on your controller, this way if it does not match it will pass to the next route.
You can see an example of and advanced constraint here:
http://blogs.planetcloud.co.uk/mygreatdiscovery/post/Custom-route-constraint-to-validate-against-a-list.aspx

How does MVC routing understands the URL?

Global.asax.cs has the following code on initialization:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
What I'm asking is, how does it know that what it gets for "{controller}" will be the name of the Controller class to be invoked? Are there tokens defined somewhere? if so, can I list them?
If I define additional tokens (like "{lang}") will it assume they are additional parameters?
(I'm developing a custom URL rewrite/redirect handler, and I need it to work with MVC...)
What is the most practical way to define custom patterns and "aliases" for URLs?
The Mvc runtime has the controller and action tokens hardcoded. In addition there is also "area" but thats about it.
#TDaver If I define additional tokens (like "{lang}") will it assume they are additional parameters?
yes. If you define, for instance, a parameter like lang, it wil detect it. Think about like that, it will be the querystring field called lang of the page. and you can create a route for a pretyy url. Like below;
routes.MapRoute(
"Default", // Route name
"{lang}/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
so the url will be like ; http://example.com/en/home/about
Also, the most important part of routing is to understand that the routes will be picked by order. for instance, if you have multiple routes matching your current request, the first route will be picked by MVC Framework.
I reccomend you to have a look at phil haccked's RouteDebugger
Also you can create route constraints for advanced routing options as well.

Resources