Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get @Url.Action value inside a controller

Tags:

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

like image 213
greay Avatar asked Apr 13 '16 05:04

greay


People also ask

How do you give a controller a URL?

If you just want to get the path to a certain action, use UrlHelper : UrlHelper u = new UrlHelper(this. ControllerContext. RequestContext); string url = u.

How do you define area in URL action?

write area name as html attribute with anonymus object. you can use actionlink html helper extension method to achieve same thing.

What is difference between HTML ActionLink and URL action?

Yes, there is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.


1 Answers

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.

like image 79
The Muffin Man Avatar answered Sep 24 '22 14:09

The Muffin Man