Get route name from inside view? - asp.net-mvc

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.

Related

Rendering action from base controller, getting route not defined error

I have a base controller all my other controllers inherit from, inside the base controller are 2ActionResult that is common to every page on my site. When I try to use Html.RenderAction to call either of these actions from a View I get the error
No route in the route table matches the supplied values.
I have checked and the Action is set to public and the names are spelled right. What am I doing wrong?
The call: #{ Html.RenderAction("SectionNavigation"); }
The Action:
[Route("SectionNavigation")]
public ActionResult SectionNavigation()
As you can see I am also using Attribute Routing as well. I also tried updated the call to specify Base as the controller but that did not change anything.
The first parameter of the RenderAction method is the action name, not the route name.
If you're rendering a View that is being generated using a Controller other than the one that defines the SectionNavigation Action, you'll have to specify the Controller name as well:
#Html.RenderAction("SecondNavigation", "YourControllerName").
If you're using Areas, you'll have to include the Area name as well:
#Html.RenderAction("SecondNavigation", "YourControllerName", new { Area = "MyAreaName" }).
See Documentation

Composite C1 Custom Routing - Creating custom routes

I am trying to figure out how to create custom routes in composite C1. I have been searching around for hours and came up with noting. I realise you can use pathinfo to slightly customise the route but this really just doesnt do what I need. I basically want to add a few custom routes to override composite C1 if there are any matches
For example I need to map
www.domain.com/job-detail/Executive_Management/HR_Executive/136307 or
www.domain.com/job-detail/Executive_Management/HR_Executive?job=136307
to
www.domain.com/job-seekers/job-search/job-detail?job=136307
Please help. I really like Composite C1 but when it comes to custom routes I find it hard to find any help online.
I figured this one out by having a further dig through the code. I noticed I could add custom routes to the App_Code\Composite\AspNet\MvcPlayer\Route.cs class.
I added this line of code to the RegisterRoutes method....
routes.MapRoute("Job-Listing", "job-detail/{category}/{title}/{id}", new { controller = "JobSearchModule", action = "JobDetail" });
I added this directly above the default route which is important
routes.MapRoute("Default","{controller}/{action}/{id}",new { action = "Index", id = "" });

Grails Custom Scaffolding get access to controller name

I am trying to write a custom src/templates/scaffolding/Controller.groovy and was wondering if there was any way to get access to the controller name? Right now it seems like you can only get the "model" class. The reason I need it is I am customizing the render to prefix the templates directory based on the controller.
For instance I have a controller named AuthorAdminController and I need to customize the list to use the /admin/user/** directory.
Let me know if you have any questions. I am getting ready to look into how to customize DefaultGrailsTemplateGenerator but I am not sure if that is the correct route to go.
Example:
class UserAdminController {
static scaffold = User
}
Currently in my Controller.groovy I get className='user' so I have no access to the controller.
I don't think you can, as the way scaffolding works your template will always be generating a class named DomainClassNameController (i.e. UserController in your example), which gets loaded into a new classloader and then the metaclass of the real controller (UserAdminController) gets new actions added to it which delegate to an instance of the generated UserController.
Now every controller has access to the controllerName property during execution of actions, so this may provide you with a workaround. I haven't tried it, but you could try putting a log.info("controller: \${controllerName}") into the template and see which name it gives you (the backslash to make it resolve at runtime rather than generation time).

adding Route name in routes.insert function 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.

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.

Resources