1

I am using MVC 3, i am passing some url in the string type property, for this i have used

var uri = new UrlHelper().Action("ActionName", "ControllerName");

But it is giving error. which is

{"Value cannot be null.\r\nParameter name: routeCollection"}

i know .Action(actionName, controllerName, routeValues, scheme); has four parameters but i only want to pass two, what will be the default value for it??

tereško
  • 58,060
  • 25
  • 98
  • 150
Syed Uzair Uddin
  • 3,296
  • 7
  • 31
  • 47
  • I have also tried this but still it is giving error "{"Value cannot be null.\r\nParameter name: routeCollection"}" var uri = new UrlHelper().Action("Actionname", "ControllerName", new RouteValueDictionary(new { Id = 4})); – Syed Uzair Uddin Mar 11 '16 at 08:07

2 Answers2

3

If you're inside your Controller, you use the static Url.Action()instead, and it will work:

var uri = Url.Action("ActionName", "ControllerName");

And if you're outside of a Controller(e.g in your Model) you have to use the UrlHelper as you did, but passing a Context in parameter, so the method can make the correct url for you.

You can get the request context in this way:

HttpContext.Current.Request.RequestContext

So, if you can use it like this:

UrlHelper url = new UrlHelper(HttpContext.Current.Request.RequestContext);
var uri = url.Action("ActionName", "ControllerName");
Thiago Ferreira
  • 661
  • 3
  • 19
0

i searched it and found the answer. Same Question

var uri = new UrlHelper(HttpContext.Current.Request.RequestContext).Action("ActionName", "ControllerName");

It is working fine after adding HttpContext.Current.Request.RequestContext.

Community
  • 1
  • 1
Syed Uzair Uddin
  • 3,296
  • 7
  • 31
  • 47