I am using ASP.net core
I can use an Html action inside a view
@Url.Action("GetOptions", "ControllerName", new { id="1"});
However I want to get a string value of it in the Controller.
e.g. something like
string Url= Url.Action("GetOptions", "ControllerName", new { id="1"}).ToString();
In previous versions of MVC you can reference the helper in the controller by
UrlHelper urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
Basically what I want to do is generate a URL string representation in my controller
If you just want to get the path to a certain action, use UrlHelper : UrlHelper u = new UrlHelper(this. ControllerContext. RequestContext); string url = u.
write area name as html attribute with anonymus object. you can use actionlink html helper extension method to achieve same thing.
Yes, there is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.
In order for the route values to work correctly for me I had to use the static invocation of the url helpers
UrlHelperExtensions.Action(Url, "Details", "Resellers", new { id = 1 })
Edit: The shorthand way of writing this is:
this.Url.Action("Details", "Resellers", new { id = 1 })
Thanks @Learner.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With