adding Route name in routes.insert function MVC - asp.net-mvc

Hi I am creating a RouteBase object and try to insert in routes collection using route.insert
routes.Insert(0, objRoute);method. How can i assign a name to the route in this scenrio??
like routes.add("myfavroute", localizedRoute); method
Info: I have to use the routes.Insert method as i have to ad the route at the top of the collection

I am afraid that you cannot provide a name if you use the Insert method to add the route. You will have to call the Add method before any other route registrations if you want this named route to be the first one.

Related

Get route name from inside view?

Using Attribute Routing you can define a route name. Is there a way I can get the name of the used route from inside my view?
According to this answer, you can use:
#{ var x = ViewContext.RouteData.Values["Action"]; }
to get route data. Then according to the right answer of this post: "How to get a custom attribute from object instance in C#", you can pull out the attributes.
This doesn't sound like a real good idea. You should probably add the route name to view data or the view model instead.
MapMvcAttributeRoutes has multiple overloads which expects a parameter for IDirectRouteProvider implementation. This interface is responsible for generating routes. The default implementation for this interface is DefaultDirectRouteProvider. Create a custom class that inherits from DefaultDirectRouteProvider and override required methods like GetActionDirectRoutes or GetControllerDirectRoutes. In this method you will get RouteEntry which contains the route name. You can add this name to datatokens.

MVC Dynamic Action in Route

Is it possible to define a route in MVC that dynamically resolves the action based on part of the route?
For example:
`/products/create/widget`
Would resolve to ProductsController.CreateWidget(Widget);
I want the route to be dynamic:
routes.MapRoute(
"Create",
"/products/create/{productType}",
new { controller = "Products", action = "Create{productType}" }
);
I need to have multiple Create actions that take in different model types but I don't want to add a new route every time I add one. Without appending the name to the action I get an ambiguous method error. Is it possible to do this?
I think you probably need to create your own custom route object derived from the RouteBase where you can assign action based on particular part of the Url. Take a look at this example.

ASP.NET MVC Routes: Conflicting action names with custom route

i all,
In a previous question, I asked how to define a custom route to handle the following URL:
http://www.example.com/User/Profile/Edit/{userProfileID}
I have a User object and a UserProfile object, but only a UserController that I want to be able to use for actions on both objects. I already have a method in UserController called Edit that handles edits on a User. But I also need a method for edits on a UserProfile. The answer to my routing question was the following route:
routes.MapRoute(
"ProfileleRoute", // Route name
"User/Profile/{action}/{userProfileID}", // URL with parameters
new { controller = "User", action = "Index" } // Parameter defaults
);
But given that custom route, where should I be declaring the edit action for a UserProfile, and what should it be called? It seems like I couldn't write another method in UserController called Edit because I already have one that handles User edits.
So I feel like I would end up with a need for two Edit actions to handle the following routes: "User/Edit" and "User/Profile/Edit". How do I get around this?
Thanks very much.
When the framework it's going to select what action to execute it first check the actions with the name required with a HttpPost ot HttpGet attribute that match the request, if not action is selected this way, then it select any action that match the name.
So, if you have two actions with the same name with no HttpPost or HttpGet attributes, you can't control with action is going to get executed.

Create a Route Constraint that only applies a route when the action has a particular action filter

I have a list of actions on various controllers that are 'Admin' functions (create, update, delete) but other actions on those same controllers that aren't admin actions.
What I want to do is create a route that will prefix /Admin/ before all urls that call an action that have the Authorize filter attribute.
Is this even possible to do?
Yes everything is possible, but I think what you mean to say is it easy to do? And the answer is no. What you have to do is create your own route, and then add this customized route to the route mapping. This isn't hard to do, but the problem arises in that the routes are initialized before the controller, so you will have to handle the lookup and reflection yourself to check for your criteria.
There is an alternative option, you can try to use the ActionMethodSelectorAttribute which allows you to create custom selectors for your Action Methods and ignore ones that don't contain the Authorize attribute. An example of this attribute being used is the ActionVerbAttribute.
The easiest way by far is to just create a custom extension for the Html.ActionLink that does its own checks and keep it as a display only thing, and then create dual routes for the same controller in your Global.asax.

How can I implement Dynamic Routing with my choice of URL format?

My URL requirement is countryname/statename/cityname. For this I'm writing my own RouteHandler and adding one new route into the routecollection like this:
routes.Add(new Route("{*data}",
new RouteValueDictionary(new
{
controller = "Location",
action = "GetLocations"
}),
new MyRoutehandler()));
Now my question is: How do I generate this type of URL?
I tried Html.ActionLink() but it's asking for an action name and controller name. However, in my URL format I don't have any action name or controller name. How do I solve this?
Is there a reason you can't just use the following with the built-in route handler:
routes.MapRoute("LocationRoute",
"{countryname}/{statename}/{cityname}",
new { controller = "Location", action = "GetLocations" });
That may conflict with the default "{controller}/{action}/{id}" route, but that would happen with your current system anyway (unless you make a custom Route, inheriting from System.Web.Routing.RouteBase, rather than a custom Route Handler). The Route Handler is for handling what to do AFTER the route data has been extracted. Since you're still using Controller and Action, you should still be able to use the MvcRouteHandler. If you want to customize how the Route Data is extracted, you probably want a custom Route.
Btw, if you want to use a route which doesn't involve Controller and Action names, use Html.RouteLink. ActionLink is just a wrapper which puts the controller and action into the RouteValuesDictionary and calls the same internal helper as RouteLink.
Although in this case, the route still has a Controller and Action ("Location" and "GetLocations"), so you can still use ActionLink. I think ActionLink lets you specify a Route Name, so if you give your route a name you can specify that name in ActionLink and it will use that route to generate the URL (I may be wrong about that and if so you can still use RouteLink and manually add the controller and action route values)

Resources