有没有办法从代码中获取完整的Url到其他控制器方法?

问题描述 投票:0回答:1

我有Asp.Net核心Web应用程序。使用以下控制器

[Produces("application/json")]
[Route("api/[controller]")]
public class TestController
{
    [HttpGet]
    [Route("firstroute")]
    public async Task<IActionResult> FirstMethod()
    {
        ...Some code...
    }

    [HttpGet]
    [Route("secondroute")]
    public async Task<IActionResult> SecondMethod()
    {
        ...
        SomeMethod(redirectLink)
        ...
    }          
}

我需要的是完全组装redirectLink到FirstMethod(它可能类似于:“http://localhost/api/test/firstroute”)。我不需要RedirectToAction,但确切地说Url是字符串。没有设法在this.Url或Microsoft.AspNetCore.Http.Extensions中找到任何合适的方法。

this.Request.GetDisplayUrl()以适当的格式返回结果,但仅针对被调用的方法。

c# asp.net-core .net-core
1个回答
0
投票

您可以使用HttpContext.Request中的数据,如下所示

var Url = string.Format("{0}://{1}{2}", HttpContext.Request.Scheme,HttpContext.Request.Host,"/api/firstroute");

和重定向

return Redirect(Url);
© www.soinside.com 2019 - 2024. All rights reserved.