ASP.NET MVC long Route issue - asp.net-mvc

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.

Related

MVC routes with different actions when path has similar pattern

In my MVC app I have two paths with a similar pattern and my route config file has methods like below.
routes.MapRoute(
name: "Route1",
url: "bookings/{username}",
defaults: new { controller = "Booking", action = "UserBooknigs" }
);
routes.MapRoute(
name: "Ruote2",
url: "bookings/{username}/{id}",
defaults: new { controller = "Booking", action = "LoadBooking" }
);
whichever path I access it calls both actions. How can I avoid this issue?

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 MVC4 - Different routing for 2 controllers with actionLinks

Okay, I've got this case where I have two controllers:
HomeController
MathController
I want the routing for my HomeController to stay as default:{controller}/{action}/{id}. But I want to access the actions in the MathController with http://myurl/Task/Math/{action}.
So what I've done is to write my RouteConfig like this:
routes.MapRoute(
name: "Math",
url: "Task/{controller}/{action}",
defaults: new { controller = "Math", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Hem", id = UrlParameter.Optional }
);
When using the above configuration and manually entering the URLs in the browser both routing methods are working. Though when trying to add a "actionLink" it always uses the Task/{Controller}/{Action} route. Even if I'm creating a link for the Home controller like this: #Html.ActionLink("Hem", "Hem", "Home", null, new { #class = "navbar-brand" })
How do I configure either my routing or my action links so that I'll get the preferred functionality?
Routes match from top down in RouteConfig.cs. Your problem is that both route configs are "catch all" routes, which means both work for any controller/action. When you use #Html.ActionLink, MVC will render the url based on the 1st route it finds, which matches your "Task" path. There are few ways to change this to get what you want.
If you want to only use the "Task" path for the Math controller, then I'd change your route to this:
routes.MapRoute(
name: "Math",
url: "Task/Math/{action}",
defaults: new { controller = "Math", action = "Index" }
);
If you want to use several controllers for the "Task" path, then you can add a route constraint. You can use it like below and specify a list of controllers (regex), or you can create your own custom Route Constraint class and implement whatever functionality you want.
routes.MapRoute(
name: "Math",
url: "Task/{controller}/{action}",
defaults: new { controller = "Math", action = "Index" },
constraints: new { controller = "Math|OtherController" }
);
Or if you want to keep all controllers/actions matching both urls, then you have to flip your routes to get the default route to display first, or you can use #Html.RouteLink like so:
#Html.RouteLink("Hem", "Default", new { controller = "Home", action = "Hem" }, new { #class = "navbar-brand" })

Change Index of Directory

I'm working on an ASP.NET MVC web application. By default, browsing to the root of a directory seems to call the controller's Index() method. Is there way to change which method is called by default here? I know I could probably name the method I want to call "Index" and it would likely work, but I'd like to know if there's a way to point the directory root to a method that I choose.
For example: mysite.com/MyDirectory/ will call Index(), which is effectively browsing to mysite.com/MyDirectory/Index. I'd like to change it so that mysite.com/MyDirectory/ calls Details, (or "browses" to mysite.com/MyDirectory/Details).
Just change the action in the default route. Something like this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Details", id = UrlParameter.Optional } // Parameter defaults
);
You could specify the behavior in your routes. If you are using the latest version of MVC, that would be in your \App_Start\RouteConfig.cs
You would have something like:
routes.MapRoute(
name: "MyDirectory",
url: "MyDirectory",
defaults: new { controller = "MyDirectory", action = "Details" });
You would place this before your default route, as your route table acts kind of like a switch statement matching on the first route that it finds.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "MyDirectory",
url: "MyDirectory",
defaults: new { controller = "MyDirectory", action = "Details" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}

How can I set up a route just for my Account controller in ASP MVC5?

I have the following route:
http://127.0.0.1:81/Account/Login
My route controller looks like this:
routes.MapRoute(
"Account",
url: "Account/{action}/{id}",
defaults: new { controller = "Account" }
);
Can someone tells me if this is the correct way to set up a route. Do I need to even specify defaults: new { controller = "Account" } ?
You have to specify controller as well as action
routes.MapRoute(
"Account",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);

Resources