0

Description


I am using only Attribute Routing in one of my MVC Controllers:

[RoutPrefix("Groups")] // NOTE: I want to use "Groups" with an "s" here.
public class GroupController : Controller 
{
    [HttpGet]
    [Route("Edit/{id}")]
    public ActionResult Edit(Guid id)
    {
        //...
    }

    [HttpPost]
    [Route("Edit")]
    public ActionResult Edit(GroupEditViewModel model) 
    {
        // ...
    }
}

In the Razor view, whenever using helpers such as:

@Html.ActionLink("Text", "Edit", new {controller = "Groups", id = "someId"})

or

@Html.BeginForm(actionName: "Edit", controllerName: "Groups")
{
}

The routes generated by the helper function are null:

<a href>Text</a>

and

<form action></form>

Question


  • Is this happenning because the controller's name is unknown maybe?
  • Is there a way I can set the name of the controller with using Only Attribute Routing ?

2 Answers 2

4

Common practices known for attribute routing are setting RoutePrefix with same name as controller's name:

[RoutePrefix("Group")]
public class GroupController : Controller 
{
    // ...
}

However, as I had seen here, custom name(s) can be set without altering/removing original route:

[RoutePrefix("Groups")]
public class GroupController : Controller 
{
    // ...
}

Remember that RoutePrefix attribute only giving alternative way to access corresponding controller, not changing its original name.

Route Prefixes are nothing but the prefix for any route that we want to apply, all we need to do is to define the route prefix on a controller so that all the action methods inside it can follow the prefix.

OTP, since "Groups" is just a RoutePrefix parameter value instead of controller class, all HTML helpers used "Groups" as controller name will return null value on respective HTML attributes.

Thus, original controller's name must be used instead of RoutePrefix value:

@Html.ActionLink("Text", "Edit", new {controller = "Group", id = "someId"})

@Html.BeginForm(actionName: "Edit", controllerName: "Group")
{
}

or use MapRoute inside RegisterRoutes method to manipulate URL together with original controller's name:

// note that "Group" controller declared with "Groups" as part of relative URL
routes.MapRoute("Groups", "Groups/{action}/{id}", new { controller = "Group", action = "Edit", id = UrlParameter.Optional });

To activate RoutePrefix effect when routing, MapMvcAttributeRoutes() should inserted into RegisterRoutes method.

CMIIW.

Sign up to request clarification or add additional context in comments.

1 Comment

How can this be done using attribute routing in WebAPI 2.0?
0

The route prefix attribute, [RoutePrefix("Groups")], controls the resulting URL. MVC, however will always use the class name as the name of the controller.

They don't have to be the same. You can simply use the route prefix to control the resulting URL and use the class name as the controller name where required and the resulting URL will be correct.

In your example:

[RoutePrefix("Groups")] 
public class GroupController : Controller 
{
    [HttpGet]
    [Route("Edit/{id}")]
    public ActionResult Edit(Guid id)
    {
        //...
    }

    [HttpPost]
    [Route("Edit")]
    public ActionResult Edit(GroupEditViewModel model) 
    {
        // ...
    }
}

The following

@Html.ActionLink("Text", "Edit", new {controller = "Group", id = "someId"})

would result in a link that looked like this (note the "Groups" in the URL despite the "Group" controller name):

<a href="/Groups/Edit/someId">Text</a>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.