I have a request controller that is getting out of hand, and I want to divide the actions on several controllers while maintaining a clean URL. I'm trying to experiment with routing, but without success. I've read some examples and tutorials on routing, but, though I understand the examples, nothing seems to apply to my case, and I feel non the wiser. What I want is for the URL Requests/Approval to be handled on my ApprovalController instead of my RequestController, so I wrote the following.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Approval",
"Request/{controller}/{action}",
new { controller = "Approval", action="Index", id = "" }
);
}
But it's not working. Why? I have a folder in the my Views called Approval, and in there I have a file called Index.cshtml. How should I code the MapRoute?
Edit
I added all the routes I've got
You need to swap the two MapRoute statements, like so:
routes.MapRoute(
"Approval",
"Request/Approval/{action}",
new { controller = "Approval", action="Index", id = "" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
The reason it is currently not working is because the first statement ('Default' route name) is getting matched before the second one is even evaluated.
In addition (as noted in my above example,) you need to remove '{controller}' in the Approval route and replace with 'Approval'... unless you specifically want the URL /Request/{ANY controller}/{action} to go through, which I doubt. From your question it seems you only want /Request/Approval/ to go to your Approval controller.
Don't forget to keep the Default route at the bottom, so as to match your other controllers and actions. It serves as a catch-all should no other matches exist.
The order you map your routes matters. Move the second route before the default route.
You will still have a problem though, as any thing /request/something will look for the SomethingContoller. To fix this, change your route to this:
routes.MapRoute(
"Approval",
"Request/Approval/{action}",
new { controller = "Approval", action="Index", id = "" }
);
Related
This is related my question which i asked in this link correct me on url routing in mvc
Now i came with another problem, so i thought i will ask it as new question.
Now i have following routes in my global.asax file
routes.MapRoute(
"Custom", // Route name
"{action}/{id}", // URL with parameters
new { controller = "Authentication", action = "BigClientLogin", id = UrlParameter.Optional } // Parameter defaults
);
and
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Authentication", action = "BigClientLogin", id = UrlParameter.Optional } // Parameter defaults
);
Now what happens is when i run my solution the URL i am getting is http://localhost:65423/Login this is what i need for my Login Page that is OK. But when i login in as user i am getting "The resource cannot be found" error.
when i checked it i can see that my URL is now changed to http://localhost:65423/Admin/Dashboard
So i think this causing the issue. So this looks the problem related to my global.asax routing.
Can anyone help me to find out what i did wrong.
You have 2 routes with completely optional segments. The issue is that there is no way for the routing framework to differentiate between them.
The only way you can make it work with your existing routes is to specify them explicitly by name (such as when using #Html.RouteLink or #Html.RouteUrl).
#Html.RouteLink("Custom Link 1", "Custom", new { action = "BigClientLogin" })
#Html.RouteLink("Custom Link 2", "Custom", new { action = "Action2" })
#Html.RouteLink("Home Page", "Default", new { controller = "Home", action = "Index" })
#Html.RouteLink("About", "Default", new { controller = "Home", action = "About" })
Doing it that way will function, but is not normal. Typically, there is only one route configured with all defaults for the controller, action, and id and the rest have some explicitly declared segments and/or constraints (segments being preferable).
routes.MapRoute(
"Custom", // Route name
"Custom/{action}/{id}", // URL with parameters
new { controller = "Authentication", action = "BigClientLogin", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Authentication", action = "BigClientLogin", id = UrlParameter.Optional } // Parameter defaults
);
The first route will now only match when the URL starts with /Custom/. If it does not start with custom, it will match the default route.
The trick is to ensure that the routes are listed in the right order and that they only match the URL in specific cases, letting them pass on to the next route in the list if the case is not correct.
It happens because of 2 things.
1st the sequence of Your route
2nd your defaults
So if you end up on 'admin/dashboard' that's not a default, guess you've just put it in your studio to start there?
To get to http://localhost:65423/Login you'll need an action on Your AuthenticationController called 'Login' but it looks like the one You have configured is 'BicClientLogin' so You won't be taken to login unless you specify it, and at that time it has to exist.
To Help You further we need to know what controllers You have and what actions there exists, plus if security is part of your solution and if in case, what is is set to use.
If I have the following MapRoute:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
I can have an url like:
blabla.com/Home/Index/123
But what kind of MapRoute do I need to create to be able to do this:
blabla.com/Home/123 or blabla.com/Home/DEADBEEF?
I imagine it involves something along the lines of
"{controller}/{id}/{action}"
Action and id are reversed, and maybe there should be a default action. But how will the MapRoute know which controller should be treated like this?
You probably need something along these lines.
routes.MapRoute(
name: "DefaultRoute",
url: "Home/{action}",
defaults: new { controller = "Home", action = "Index" },
constraints: new { action = "[A-Za-z]*" }
);
or without an action
routes.MapRoute(
name: "DefaultRoute",
url: "Home/{id}",
defaults: new { controller = "Home", action = "Index", id = "" },
constraints: new { id = "[A-Za-z]*" }
);
You will need to make sure that the route name is different than any other routes you have setup and pay attention to the ordering of the routes as other routes that are similar can override each other. In this case make sure you would probably want this before the default route but be aware that it will override it.
As for the not having controller or even the action you can set defaults and do not need them within the route.
As for constraints you can simply add the constraints parameter to the route to set a regular expression for a certain attribute as shown above.
EDIT:
Here are some useful links for more info on routing if you need it.
http://www.dotnet-tricks.com/Tutorial/mvc/HXHK010113-Routing-in-Asp.Net-MVC-with-example.html
http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx
Advanced ASP Routing tutorials and examples
You can add a route like this:
routes.MapRoute(
"CustomRoute",
"Home/{id}",
new { controller = "Home", action = "Index", id = "" });
Make sure you place it above the default route. However this route will block all the other actions in HomeController. Since we don't have a constraint on id parameter, the framework can't possibly know if you are referring to an action or an id in your URL. Since this route comes first, it will map it to id parameter.
I'm writing an MVC3 application that will need to make use of URL rewriting in the form of http://[server]/[City]-[State]/[some term]/ .
As I understand it, MVC3 contains a routing engine that uses {controler}/{action}/{id} which is defined in the Global.asax file:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
Traditionally (in a non-MVC app), I would use some URL rewriting flavor to decode a url such as http://www.myserver.com/City-State/somesearch/ to querystring parameters that look something like this:
http://www.myserver.com/city=City&state=State&query=somesearch
Keep in mind that this request would be coming from http://www.myserver.com/Home
Can this can be accomplished without having to specify a controller... something like this:
routes.MapRoute(
"Results",
"{city}-{state}/{searchTerm}",
new { controller = "Results", action = "Search" }
);
... or is it really best to have the controller listed?
How do you handle this in an MVC3 environment?
Thanks.
URL rewriting in asp.net MVC3:-
you can write code for url rewriting in Global.asax file :-
//Default url
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"",
new { controller = "Home", action = "Index", id = "" }
);
//others url rewriting you want
RouteTable.Routes.MapRoute(null, "Search/{City_State}/{ID}", new { controller = "Home", action = "Search" });
Check out these two answers:
ASP.NET MVC Routes: How to define custom route
Defining custom URL routes in ASP.Net MVC
Summary:
Specify custom routes before the default one.
Define specific routes before general as they may match both.
Default values are optional.
Specify default Controller and Action in the default parameter object.
You can do this by registering route in Global.asax file, but order to register the Route is important you must be register first Old route then new one.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// for Old url
routes.MapRoute(
"Results",
"{city}-{state}/{searchTerm}",
new { controller = "Results", action = "Search" }
);
// For Default Url
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
I need to access a parameter without name.
Ex: I have a controller test, I want to get "data" in mysite.com/test/data. Without calling the action data.
I know how to do that passing it though Index action.
public ActionResult Index(string id)
That way I'd only need to type mysite.com/test/Index/data to get "data", but I don't want to have to type Index.
Does anyone know how to do it?
EDIT:
Thanks a lot to #andyc!
AndyC I used what you said and created a test. It worked =D
Now I can type mysite.com/something and if something is not an controller it redirects to my profile page.
This is what is working for me
routes.MapRoute(
"Profile",
"{id}",
new { Controller = "Profile", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Setup a custom route
In your global.asax file, put (Inside the RegisterRoutes method):
routes.MapRoute(
"MyShortRoute",
"view/{id}",
new { Controller = "test", action = "Index" }
);
The first parameter is a name, the second is the URL format and the last parameter is the default values (in this case if you leave id empty it'll default to id 0.
Notice that I don't use test/{id} as this would also match test/Edit, where edit is an action that you do not want passed as a parameter (I can't think of another way to avoid this, especially if you're using strings instead of ints for your parameters).
Remember to order your routes appropriately in your global.asax file. Put more specific routes before less specific routes. When the system is searching for a route to take, it does not attempt to find the most specific match but instead it starts from the top, and uses the first match it finds.
Therefore, this is bad:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "test", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Specific",
"test/{id}",
new { controller = "test", action = "Index", id = 0 }
);
In this example, if you browse to test/somevalue it will match the FIRST entry, which is not what you want, giving you the testcontroller and the somevalue action.
(As your adding a more specific route, you will want it near the top, and definitely before your default).
I am learning MVC and I need to understand why it doesn't work the way it should.
Here is my routing :
public static void RegisterRoutes(RouteCollection routes)
{
// Note: Change the URL to "{controller}.mvc/{action}/{id}" to enable
// automatic support on IIS6 and IIS7 classic mode
//http://localhost/store/category/subcategory/product
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Category", // Route name
"store/{category}/{subcategory}", // URL with parameters
new
{
controller = "Catalog",
action = "Index",
category = "Featured Items",
subcategory = "All Items"
}
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" }, // Parameter defaults
new { controller = #"[^\.]*" } // Parameter constraints
);
}
The way I understand routing I should see the following url when I start the web app :
http:/localhost/store/
What I get is the second route....
Furthermore if I change the second route to "home/{action}/{id} it doesn't catch any routes.
Could you help me understand this please..Thanks
Routes do not specify default URL; the default URL is handled by your app. Routing specifies that when it sees http://localhost/store/bikes/mountain, it will use the catalog controller. But that doesn't specify the default URL; you have to enter that in the project properties.
I would recommend not changing the second one because unless you are creating groupings for all of your controllers, it's best to have the default as it is so you can catch all URL's. Your change to the second one would require the url to be:
http://localhost/home/home/index to match the HomeController's index action, whereas the default setup catches http://localhost/home/index...
Does that make sense?
Try this: http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx