0

I have Asp.Net Core web application. With following controller

[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)
        ...
    }          
}

What I need is to get fully assembled redirectLink to FirstMethod (it will probably be similar to this: "http://localhost/api/test/firstroute"). I need not RedirectToAction, but exact Url as string. Didn't manage to find any suitable methods in this.Url or Microsoft.AspNetCore.Http.Extensions.

this.Request.GetDisplayUrl() returns result in appropriate format, but only for the called method.

  • 1
    First I would ask *why* you want this, it's much more common to be doing these things from Razor code. Having said that, there is a `Url` property (that gives you a `UrlHelper` object) of the controller (assuming you inherit from `Controller` properly) – DavidG Aug 31 '18 at 13:27
  • I need to implement Google Authorization for Google Sheets API by hands. Because Google's GoogleWebAuthorizationBroker which hides this process calls to Windows specific methods. Due to that it doesn't work on my Linux server. For authorization I need that calls for getting authorization code and changing code to token would have same "redirectLink" parameter passed to them. – A. Pozdnyakov Aug 31 '18 at 13:32
  • Possible duplicate of [Getting full URL of action in ASP.NET MVC](https://stackoverflow.com/questions/2005367/getting-full-url-of-action-in-asp-net-mvc) – Matt Aug 31 '18 at 13:32

1 Answers1

0

you can use data from HttpContext.Request like bellow

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

and rediret by

return Redirect(Url);
siasty
  • 167
  • 1
  • 2
  • 10
  • Viable, but I'm still need to hardcode "/api/firstroute". I woul'd like to avoid it as much as possible. – A. Pozdnyakov Aug 31 '18 at 14:48
  • you can get full address from HttpContext but you need use global variable in controller and when you invoke firstethod add value to variable. – siasty Aug 31 '18 at 17:37