I have a simple API with basic routing. It was setup using the default Visual Studio 2015 ASP.NET Core API template.
I have this controller and action:
[Route("api/[controller]")]
public class DocumentController : Controller
{
[HttpGet("info/{Id}")]
public async Task<Data> Get(string Id)
{
//Logic
}
}
So to reach this method, I must call GET /api/document/info/some-id-here
.
Is it possible with .NET Core, inside that method, to retrieve as a string the complete route?
So I could do for example:
var myRoute = retrieveRoute();
// myRoute = "/api/document/info/some-id-here"
You can also ask MVC to create a new route URL based on the current route values:
Url.RouteUrl
is a helper method that lets you build a route URL given any route values.RouteData.Values
gives you the route values for the current request.You can get the complete requested url using the Request option (HttpRequest) in .Net Core.
Your final code.
Result route: "/api/document/info/some-id-here" //for example