MVC6 attribute routing with two parameters

si2030 picture si2030 · May 6, 2016 · Viewed 7.2k times · Source

I've had a look around for this and nothing that pertains to the MVC6 taghelper anchor tag in relation to having an alternative [HttpGet] method that caters for multiple parameters.

Sure you can add multiple parameters to a MVC6 anchor taghelper but how do you process the second option with two parameters using attrubute routing...

I have two [HttpGet] IactionResult methods:

    //GET: UserAdmin
    public async Task<IActionResult> Index()
    {
        return View(await _userAdminService.GetAllUsers("name_desc", false));
    }


    // GET: UserAdmin/name_desc/True
    [HttpGet("Index/{sortValue}&{showDeactivated}")]
    public async Task<IActionResult> Index(string sortValue, bool showDeactivated)
    {
        return View(await _userAdminService.GetAllUsers(sortValue, showDeactivated));
    }

I have in my view an attempt to go to the second method:

<a asp-action="Index" asp-route-sortValue="@Model.DisplayName" asp-route-showActivated="@Model.ShowDeActivated">Name: <span class="glyphicon glyphicon-chevron-down"></span></a>

which renders to:

<a href="/UserAdmin?sortValue=name showActivated=True">Name: <span class="glyphicon glyphicon-chevron-down"></span></a>

or

    localhost.../UserAdmin?sorValue=name&showActivated=True

IT never goes to the second method.

What do I need to do to use the second [HttpGet] method with two parameters using the MVC6 anchor taghelper?

EDIT

Also how do you handle the ampersand separating the two parameters in the route attribute...

Answer

Mike picture Mike · May 6, 2016

There is no support for ampersand in route template. The idea is that ampersand is used for query string and it will always be applied to any route template. That's why your second action is never called.

For example you can change your route template to [HttpGet("UserAdmin/Index/{sortValue}/{showDeactivated}")]

Official documentation link