How to get @ Url.Action value inside the controller

I am using ASP.net core

I can use the html action inside the view

 @Url.Action("GetOptions", "ControllerName", new { id="1"}); 

However, I want to get the string value in the controller.

eg. sort of

 string Url= Url.Action("GetOptions", "ControllerName", new { id="1"}).ToString(); 

In previous versions of MVC, you can refer to the helper in the controller on

  UrlHelper urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext); 

Basically what I want to do is generate a URL string view in my controller

+11
source share
2 answers

In order for route values ​​to work correctly for me, I had to use a static call to URL helpers

UrlHelperExtensions.Action(Url, "Details", "Resellers", new { id = 1 })

Edit: Shortened way to write:

this.Url.Action("Details", "Resellers", new { id = 1 })

Thanks @Learner.

+8
source

Add @using Microsoft.AspNetCore.Mvc to Views \ _ViewImports.cshtml.

I'm late to the party, but I decided to add my answer for reference anyway.

0
source

All Articles