Routing with and without controller name in ASP.NET MVC 4 - asp.net-mvc

I'm using ASP.NET MVC 4 and I have some problems settings up my routes. Could you tell my how to set up my routes to point urls to actions as follows:
"/" (or "/Start") => PublicController.Start()
"/About" => PublicController.About()
"/MyPage" (or "/MyPage/Summary") => MyPageController.Summary()
"/MyPage/Invoices" => MyPageController.Invoices()
"/MyPage/Invoice/72" => MyPageController.Invoice(int id)
It's the url "/About" that messes things up for me, i.e. a url that does not specify the controller. If I make that one work the others that do specify controller stop working. I could just create a separate controller for "/About" I guess, but I'd rather not if I don't have to (I have more urls following that pattern).

This should do it:
routes.MapRoute(
name: "About",
url: "About",
defaults: new { controller = "Public", action = "About" }
);
routes.MapRoute(
name: "MyPageSummary",
url: "MyPage",
defaults: new { controller = "MyPage", action = "Summary" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Public", action = "Start", id = UrlParameter.Optional }
);

Related

How to specify single controller +single action for n routes in asp.net MVC?

I have this initial configuration in my route.config.
routes.MapRoute(
name: "ALBUMS",
url: "albums/",
defaults: new { controller = "Templates", action = "Connections", id= UrlParameter.Optional });
Now how should I specify that anything after /albums/, like
/albums/bollywood
/albums/bollywood/hindi
/albums/tollywood
/albums/bollywood/hindi/old
/albums/bollywood/hindi/old/kishore
like this chain goes on. How to configure if url starts with /albums/ , please render
routes.MapRoute(
name: "ALBUMS",
url: "albums/",
defaults: new { controller = "Templates", action = "Connections", id= UrlParameter.Optional });
Because I don't have any bollywood,tollywood actions.
In actual, I have Welcome controller and action Selected.. thats it.
All others are templates.
try
routes.MapRoute(
name: "Album",
url: "Album/{*anything}",
defaults: new { controller = "Album",action = "Index",id = UrlParameter.Optional }
);
(if you also have the default route, this should go before it)

Asp.net mvc routing without a controller or action name

I am trying to redirect all urls that don't match an existing controller to a certain controller.
For example, the url mywebsite.com/newyork should be processed as mywebsite.com/Cities/Info/newyork
I am using the following code in my RegisterRoutes but it doesn't seem to work as I get a 404 reponse:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Cities",
url: "{cityname}",
defaults: new { controller = "Cities", action = "Info", cityname= "" }
);
You should put your cities route first and drop the empty default parameter:
routes.MapRoute(
name: "Cities",
url: "{cityname}",
defaults: new { controller = "Cities", action = "Info" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The routes are processed in order so you should have most specific first to least specific ( your default route).
As your website.com/newyork matched the default route, it wasn't continuing to your city route.

ASP.NET MVC routing to action

I'm having trouble doing what I want to achieve when doing routing in ASP.NET MVC.
What I want to do is the following:
When typing http://localhost/MyWebsite/, I want to redirect to http://localhost/MyWebsite/Login/ (The redirection to the action Index is implied here)
When typing http://localhost/MyWebsite/MyAction, I want to redirect to http://localhost/MyWebsite/Login/MyAction
Point 1 was achieved using the following lines in the file RouteConfig.cs:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional });
But I cannot accomplish point 2. Note that the user does not come from another action in a controller, but actually types the address in his browser.
Thanks for your help.
since /MyWebsite/Login and /MyWebsite/MyAction both have two segments in the URL, they're both matching your defined Route.
You can use a Route Constraint to only match /MyWebsite/Login to your first point, followed by a modified second route mapping:
routes.MapRoute(
name: "Default",
url: "MyWebsite/Login/",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional });
routes.MapRoute(
name: "Actions",
url: "MyWebsite/{action}/",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional });

Asp MVC 4 MapRoute specific url

I have web site root structure controllers for work with culture part in url,
like mysite.com/en/controller/action/id
but for one specific controller I don't want to use culture prefix.
mysite.com/controller/action/id
How I can do it ?
I have write it MyRoute but it dos't work
routes.MapRoute(
name: "MyNewRoute",
url: "MyNewRoute/",
defaults: new { controller = "MyNewRoute", action = "Data", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "MemberTransfer",
url: "en/Member/Transfer",
defaults: new { culture = "en", controller = "Member", action = "Transfer", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "ExecuteTransfer",
url: "en/Member/ExecuteTransfer",
defaults: new { culture = "en", controller = "Member", action = "ExecuteTransfer", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "",
defaults: new { culture = "en", controller = "Home", action = "Maintenance", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "HomePage",
url: "{culture}/{*pathInfo}",
defaults: new { culture = "en", controller = "Home", action = "Maintenance", id = UrlParameter.Optional }
);
Your "Default" route is overriding the last one. Try to swap the last two routes.
Btw, you can use handy route debugger from Phil Haack. It's really easy to set up and use (you just need to comment/uncomment one single line in your Global.asax.cs). It's absolutely must have tool for every Asp.Net MVC developer.
Swap your last two routes. Routes are evaluated top down, going with the first match, so you want to go from most specific to most generic in the order that you add them.

ASP.NET MVC long Route issue

How I can to avoid route config like that?
I have an admin panel where user with administrator role is able to do some action(edit/delete/view) with different entities(users/profiles/addresses/...).
And i want put all this action into 1 controller.
routes.MapRoute(
name: "EditProfile",
url: "Account/Profiles/{id}/Edit",
defaults: new { controller = "Account", action = "EditProfile" }
);
routes.MapRoute(
name: "RemoveProfile",
url: "Account/Profiles/{id}/Remove",
defaults: new { controller = "Account", action = "RemoveProfile" }
);
routes.MapRoute(
name: "EditAddress",
url: "Account/Addresses/{id}/Edit",
defaults: new { controller = "Account", action = "EditAddress" }
);
routes.MapRoute(
name: "RemoveAddress",
url: "Account/Addresses/{id}/Remove",
defaults: new { controller = "Account", action = "RemoveAddress" }
);
//...
Basically I want replace all MapRoute to smth like this:
routes.MapRoute(
name: "AccountProfileActions",
url: "Account/{entities}/{id}/{subAction}",
defaults: new { controller = "Account", action = {subAction} + {entities}}
);
How can I do this?
Just don't specify the action (or controller for that matter) and rely on MVC Route conventions, i.e. the controller name and action name will be part of the URL.
If you want to have a specific route that does not match up with your controller/action names, then consider using something like AttributeRouting, which will let you specify the route right on your controller/action, instead of having to go into RouteConfig each time.

Resources