Understanding routing in ASP.NET MVC - c#

I'm trying to wrap my mind around the way ASP.NET MVC implements routing.
From what is my current understanding, it seems my route string much have a "{controller}" and "{action}", otherwise it doesn't work?
How would I define the route that using a SearchController and Search action taking both SearchKeywords and SearchCaseSensitive arguments had the following URL?
domain/SearchKeywords/CaseSensitive
Even simpler, how do I map domain to controller SearchController and to Search?

From what is my current understanding,
it seems my route string much have a
"{controller}" and "{action}",
otherwise it doesn't work?
Values for the controller and action tokens are required. You have 2 options for providing the values:
1) Using {controller} and {action} tokens on the URL template. e.g.:
routes.MapRoute(null, "{controller}/{action}");
2) Using default values for controller and action. e.g.:
routes.MapRoute(null, "some-url",
new { controller = "Search", action = "Search" }
);
How would I define the route that
using a SearchController and Search
action taking both SearchKeywords and
SearchCaseSensitive arguments had the
following URL?
domain/SearchKeywords/CaseSensitive
The URL host (or domain) is not considered by the routing system, only the application relative path. You can do this:
routes.MapRoute(null, "{SearchKeywords}/{CaseSensitive}",
new { controller = "Search", action = "Search" }
);
You can also provide defaults for SearchKeywords and CaseSensitive, if you want to make either of them optional.

You can add controller = "Search", action = "Search" to the defaults (the last parameter).
The routing engine will use values in defaults to fill in for parameters that aren't in the URL.

If you want to have a 'domain' parameter in your route, you must put this at the top of the route registration. The 'domain' parameter in the second anonymous object is a constraint and here is set to be a regular expression that tests to see if the domain is either of the possible domains "DefaultDomain" or "OtherDomain".
routes.MapRoute("DomainRoute", "{domain}/{controller}/{action}",
new {domain = "DefaultDomain", controller = "Search", action = "Search"},
new {domain = "DefaultDomain|OtherDomain"});

Related

MVC routing - specify id without action

I would like to specify an id without having to include the index action.
Here is what I have tried:
routes.MapRoute("Upload", "Upload/{id}",new { controller = "Upload", action = "Index" });
This gives a 404 error. The url will be something like site.com/Upload/123
Probably you need to put the code before the Default route. MVC check routes from the any route after default won't work

C# MVC utilising parameter on default route

I'm using MVC 4.
My default route on my site is Home/Index so when the user enters the URL www.example.com it goes to that route.
Could you let me know if it is also possible to receive a parameter appended to that URL i.e. www.example.com/param? It works if I use www.example.com/Home/Index/param but that's not ideal.
I'm guessing its something I need to add to the Global.asax but I can't find examples anywhere.
context.MapRoute(
"Home_all",
"/{*actions}",
new { controller = "Home", action = "Index"}
);
But be aware that route will match all urls , so you need to register it at last :) That Routing will be called like this in your Home Controller for example... Actions will be a part from the url, and you can even add some parameter in the query string
public ActionResult Index(string actions, string id)
{
}

How can I route requests to a page controller for routes that don't exists in ASP.NET MVC 4

How can I route requests to a default PageController for routes that do not map to a controller or action? But for routes that do match a controller or action, have those behave as normal, using ASP.NET MVC?
Basically I have a CMS backend that I have developed and I need to be able to inspect the route, to see if it matches a route for a page, stored in the database and if so, forward the request to the default PageController, which handles loading the pages content from the database. There are also CORE pages, that do have their own Controllers and Actions defined, and if you enter a route that doesn't match a pages route in the database, I need it to revert to the default behavior of ASP.NET MVC and look for the {controller}/{action}.
I have searched and searched online, with not much luck finding how I could do this. Any help would be greatly appreciated.
Set your routes up so the ones for existing controllers and actions are first, then have a catchall that maps to your PageController:
// More specific routes go here
routes.MapRoute(
null,
"{controller}/{action}/{id}", // URL with parameters
new { action = "Index", id = UrlParameter.Optional }, // Don't put a default for controller here
// You need to constrain this rule to all of your controllers, so replace "ControllerA" with an actual controller name, etc
new { controller = "ControllerA|ControllerB|ControllerC" }
);
routes.MapRoute(
null,
"{*path}",
new { controller = "Page", action = "Index" }
);
Anything that isn't matched by the first rule and any rules you put before it will fall back to your page controller, with the path in a path parameter on your index action method.

Is there a way to make the route mapping based on specific path

I code lots of ASP.NET but I'm kind of new with .net MVC, I've a default route registered like this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
And I want to add another Administrator area on the site and all the URL would be something like "http://localhost/Administrator/controller1", "http://localhost/Administrator/controller2", etc. I've lot of controllers in the Administrator namespace and I'm trying to register those controller with only one MapRoute, I did something like this:
routes.MapRoute("Administrator_default", "Administrator/{controller}/{action}/{id}", new { controller = "Administrator", action = "Index", id = "" });
it works with those controller but one problem is that in some other controller while I try to do a redirect like:
return RedirectToAction("Index", "Forum");
Then I'll always be redirect to http://localhost/Administrator/Forum instead of http://localhost/Forum, it's not a big issue but make the URL looks strange, I tried to restrict to certain namespace but it's not working. It looks just as I'm trying to register two default route and .Net just match the first one, I'm wondering is there a way to make it two default route and map on only specific path only?
This exact issue is why Areas were added to MVC 2. http://www.asp.net/whitepapers/what-is-new-in-aspnet-mvc#_TOC3_2
Agree with Zach's answer.
Not ideal, but you do have the option to have controllers in the controller root folder (e.g. /controllers/HomeController.cs) of your project as well as the controllers in Areas (maybe high level root pages that display menus for areas).
Secondly a quick tip on using the RedirectToAction method. You can specify the area you would like to redirect too using the route parameters e.g:
RedirectToAction("Index","Form", new { area = "MyOtherArea" });

URL routing in asp.net mvc

i have a ApplicationController class with an action called Admin
so my url is www.mysite.com/Application/Admin
is there anyway i can have the routing be www.mysite.com/Admin and go to this method.
i know i can do this by creating an AdminController but for this one function i thought it was easier to just put in another controller
Put this above your default route:
routes.MapRoute(
"ShortRoute",
"{action}",
new { controller = "Application", action = "Index"}
);
You can set the Application controller and the Admin method as the default controller and action, using parameter defaults:
routes.MapRoute(
"Default", // Route name
"{action}", // URL with parameters
new { controller = "Application", action = "Admin" }
);
If this is your last route, it will match any request that does not have a controller name and an action name in it. In this particular example, even a request without an action will execute your Admin action, since it's the default action.
Note that routes with parameter defaults can create strange behavior in your existing routes, if you have any. You can always use the ASP.NET MVC Routing Debugger to test which routes match a given URL.

Categories

Resources