简体   繁体   English

无法获取 Web API 控制器方法的 URL

[英]Unable to get Web API controller method's URL

Env: C# Core 3.1 WebApi Project.环境:C# Core 3.1 WebApi 项目。

In my controller I'm trying to get the current URL but am getting a null value.在我的控制器中,我试图获取当前的 URL,但得到一个空值。 I've tried a number of things using the Url class:我使用 Url 类尝试了很多事情:

var x = Url.Link("https://localhost:44397/api/Account/Accounts/", new { a = 1, b = 2 });
var y = Url.RouteUrl("www.xyz.com", new { a = 3, b = 4 });
var url = Url.Link("GetAuthors", authorsResourceParameters);
var r = Url.RouteUrl("This route");
var yd = Url.Action("myAction", "myController", authorsResourceParameters, "https");

The Url class is from Microsoft.AspNetCore.Mvc.Routing.EndpointRoutingUrlHelper. Url 类来自 Microsoft.AspNetCore.Mvc.Routing.EndpointRoutingUrlHelper。 Is this the correct namespace?这是正确的命名空间吗?

What am I missing?我错过了什么?

You can use the below code:您可以使用以下代码:

var request = HttpContext.Request;

var uri = string.Concat(request.Scheme,"://",
                        request.Host.ToUriComponent(),
                        request.PathBase.ToUriComponent(),
                        request.Path.ToUriComponent(),
                        request.QueryString.ToUriComponent());

good luck.祝你好运。

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

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