简体   繁体   English

Url.Action无法与使用路由注释的控制器中指定的路由一起使用

[英]Url.Action is not working with routes specified in controller using route annotation

I have an AdminController that looks like the following: 我有一个如下所示的AdminController:

AdminController [Route("admin")]

Index() (landing page for all administrative content)

UserIndex() - [Route("users")]
UserDetails() - [Route("users/details/{id}")]

RoleIndex() - [Route("roles")]
RoleDetails() - [Route("roles/details/{id}")]

These equal the following URL patterns: 这些等于以下URL模式:

admin/users 管理/用户
admin/users/details/1 管理/用户/细节/ 1

However, if I try to do the following in my view it does not work: 但是,如果我尝试执行以下操作,则它不起作用:

Url.Action("UserDetail", "Admin")

shouldn't this be smart enough to output: '/admin/users/details'? 这不应该足够聪明以输出:'/ admin / users / details'吗?

It only works if I do Url.Action("Index", "Admin") since there is no route tag with it. 仅当我执行Url.Action("Index", "Admin")它才有效Url.Action("Index", "Admin")因为它没有路由标签。

Update: 更新:

I am attempting to use Kendo Template Syntax with a Kendo Grid ClientTemplate column: 我正在尝试将Kendo模板语法与Kendo Grid ClientTemplate列一起使用:

    columns.Bound(c => c.Id).ClientTemplate(
        "<a href='" +
            Url.Action("UserDetails", "Admin") +
            "/#= Id #'" +
        ">Details</a>"
    );

The link for each row ends up looking like the following: 'localhost:9000/123' The /admin/users/detail is completely being ignored... 每行的链接最终如下所示: 'localhost:9000/123'/ admin / users / detail被完全忽略...

And here is my exact declaration of the method I am trying to call in the AdminController: 这是我尝试在AdminController中调用的方法的确切声明:

// GET: Users/Details/5
[Route("users/details/{id}")]
public async Task<IActionResult> UserDetails(string id)
{
}

Recheck the routes and actions for controller 重新检查控制器的路线和动作

[Route("admin")]
public class AdminController : Controller {
    // GET admin
    [Route("")]
    public IActionResult Index() {...}

    //GET admin/users
    [Route("users")]
    public IActionResult UserIndex() {...}

    //GET admin/users/details/1
    [Route("users/details/{id}")]
    public IActionResult UserDetails(string id) {...}

    //GET admin/roles 
    [Route("roles")]
    public IActionResult RoleIndex() {...}

    //GET admin/roles/details/1
    [Route("roles/details/{id}")]
    public IActionResult RoleDetails(string id) {...} 

}

shouldn't this be smart enough to output: '/admin/users/details'? 这不应该足够聪明以输出:'/ admin / users / details'吗?

according to your routes setup /admin/users/details does not exist. 根据您的路线,设置/admin/users/details不存在。

Its waiting for a request to /admin/users/details/{id} where {id} is a userid . 它正在等待对/admin/users/details/{id}的请求,其中{id}是一个userid So when Url.Action("UserDetail", "Admin") is requested, there is nothing to match. 因此,当Url.Action("UserDetail", "Admin") ,没有任何匹配项。

Url.Action("UserDetail", "Admin", new { id = "1" })

You can make the id parameter optional by updating the route 您可以通过更新路线来使id参数为可选参数

[Route("users/details/{id?}")]

This will allow users/details to work. 这将允许users/details工作。 but the value of id parameter will default to null . 但是id参数的值将默认为null

It only works if I do Url.Action("Index", "Admin") since there is no route tag with it 它仅在我执行Url.Action("Index", "Admin")因为它没有路由标签

It only works with Url.Action("Index", "Admin") because convention matches Index by default. 它仅适用于Url.Action("Index", "Admin")因为默认情况下约定与Index匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM