简体   繁体   English

如何使用路由名称获取WebAPI路由网址

[英]How to I get the WebAPI Route Url using the Route Name

The question is quiet naive, but I couldn't find the solution online or in Microsoft's docs (or I may not be looking enough :-)) 这个问题很幼稚,但是我无法在线或在Microsoft的文档中找到解决方案(或者我可能看起来不够:-)。

So here, I have a method marked with attribute of HttpGet with a route name "GetValues". 所以在这里,我有一个标记为HttpGet属性的方法,其路由名称为“ GetValues”。 Is there a way to get the Url of the route using the route name (which I believe is the sole point of having route name). 有没有一种方法可以使用路由名称(我认为这是拥有路由名称的唯一要点)来获取路由的网址。

    [Route("api/Values")]
    public class ValuesController : ControllerBase
    {
        [HttpGet("{id}", Name = "GetValues")]
        public async Task<IActionResult> GetValuesAsync()
        {

        }
}

The simple solution is: 简单的解决方案是:

var url = Url.Link("GetValues", "Values", new { id = 123 });

But the complete url is: 但是完整的网址是:

var url = string.Format("{0}{1}", 
                        Request.Url.Authority,
                        Url.Action("GetValues", "Values", new { id = 123 }));

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

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