具有两个参数的MVC6属性路由 [英] MVC6 attribute routing with two parameters

查看:84
本文介绍了具有两个参数的MVC6属性路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经对此进行了四处浏览,但与MVC6 taghelper锚标签无关的是,它具有可替代的[HttpGet]方法来满足多个参数的要求.

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.

当然可以将多个参数添加到MVC6锚标记帮助器中,但是如何使用attrubute路由使用两个参数来处理第二个选项...

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...

我有两个[HttpGet] IactionResult方法:

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>

呈现为:

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

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

IT从不采用第二种方法.

IT never goes to the second method.

使用MVC6锚点taghelper使用带有两个参数的第二个[HttpGet]方法需要做什么?

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

编辑

您还如何处理&符号,将route属性中的两个参数分开...

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

推荐答案

路由模板中不支持&符.这个想法是&符号用于查询字符串,它将始终应用于任何路由模板.这就是为什么您的第二个操作永远不会被调用的原因.

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.

例如,您可以将路线模板更改为 [HttpGet("UserAdmin/Index/{sortValue}/{showDeactivated}")]

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

官方文档链接

这篇关于具有两个参数的MVC6属性路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆