UrlHelper.Action throws an ArgumentNullException

I use UrlHelper to create the URL, however I get an ArgumentNullException when I call the Action method (action, controller, route).

UrlHelper urlHelper = new UrlHelper(); if (!string.IsNullOrEmpty(notificacao.NotAction)) { NotRequestUrl = urlHelper.Action("myAction", "myController", HMTLHelperExtensions.convertStringToRouteValueDictionary(myparameters)); } 

I created a helper function that creates object route values ​​for me (and it works correctly).

  public static RouteValueDictionary convertStringToRouteValueDictionary(string parametros) { RouteValueDictionary dicionario = new RouteValueDictionary(); foreach (string parametro in parametros.Split(';')) if (parametro.Split('=').Count() == 2) dicionario.Add(parametro.Split('=')[0], parametro.Split('=')[1]); return dicionario; } 

The strangest thing is that it already works inside the controller, but it does not work in a separate class (for example, in BusinessLayer / Facade).

None of the arguments matter.

It is called from the Task method.

I also tried to get a context like:

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

But it HttpContext.Current returns null to me.

+4
source share
1 answer

You need to pass the current RequestContext . Otherwise, it has no way to generate the appropriate URLs for you, since it has no context:

 UrlHelper urlHelper = new UrlHelper(this.Request.RequestContext); 

The default constructor (parameter-less) is intended for use only by unit testing ( source ).

See MSDN

+4
source

All Articles