URL routing in asp.net mvc - c#

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.

Related

Issue with MVC Routing?

I have an issue in Routing in MFA where link like
Project_Name\Controller\Index is working, whereas
Project_Name\Controller\ is not working
This is happening for only some controllers after being deployed in a server.
I am getting the following error:
403 - Forbidden: Access is denied.You do not have permission to view this directory or page using the credentials that you supplied.
Is there any further configuration values that needs to be considered?
In RouteConfig we define the route path like this
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
So the controller you are using may have Index action method and the URL works.
But if your controller does not contain Index method or it is having parameters then it will not work.
This is a possible solution that you might create an action method named Index in all those controllers.
for more, you need to share some more details.

How do I change the route of an ASP.NET MVC controller?

I have a typical ASP.NET MVC controller, but I just want to change its route. The default route now is:
Blog/{controller}/{action}/{id}
I want to change the route of a specific controller to
Blog/Admin/{controller}/{action}/{id}"
I tried to achieve this by adding the Route, RouteArea and RoutePrefix attributes to the controller but without any success.
How can I achieve this?
Add this route prior to the default
routes.MapRoute(
name: "BlogAdmin",
url: "Blog/Admin/{action}/{id}",
defaults: new { controller = "YourSpecificControllerName", action = "Index or other default action name", id= UrlParameter.Optional });
Since this is for a specific you don't need {controller} part in your url. If you still want to specify it change the url argument to "Blog/Admin/YourSpecificControllerName/{action}/{id}" where YourSpecificControllerName is the name of your controller.
Also since the order of rote registration matters make sure that this route registered prior to the the default one

ASP.NET MVC 4 RedirectToAction doesn't work properly

I created an MVCApplication and it works in a directory on my server like this:
http://www.mywebsite.com/MyApp/
When I use RedirectToAction like this;
return RedirectToAction("Index", "Home");
It goes to;
http://www.mywebsite.com/Home/Index
But I want to redirect to;
http://www.mywebsite.com/MyApp/Home/Index);
How can I resolve it?
Edit---
My routeconfig is like this;
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
These redirects follow your route configuration. The default route is {siteUrl}/{controller}/{action}, which is why when you give it the "Index" action and the "Home" controller, you get the URL you're seeing.
You need to specify a new route similar to this:
MapRoute("CustomRoute", "MyApp/{controller}/{action}";
You can then redirect to it like this:
RedirectToRoute("CustomRoute", new {Controller = "Home", Action = "Index" });
EDIT to clarify for the comments -
It is also possible to solve this using IIS to make MyApp an application. This is the right solution only if MyApp is the only value you ever want at the beginning of your route for this site. If, for example, you sometimes need MyApp/{controller}/{action} and other times you need OtherSubfolder/{controller}/{action}, you need to use the routes as I have outlined above (or use areas) instead of just updating IIS.
I am assuming you want the solution using MVC, so that is what is here, but you should also consider the IIS application if that's the right solution for you.

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.

Understanding routing in ASP.NET MVC

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"});

Categories

Resources