5

I am using the UrlHelper to generate an URL, however, I am getting the ArgumentNullException when I call the method Action(action, controller, route).

UrlHelper urlHelper = new UrlHelper();

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

I've created a helper function that creat to me the object route values (and it's working properly).

    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 most strange is that it's already working inside a controller, however, it isn't working in separate a class (like a BusinessLayer/Facade).

None of the arguments are nulls.

It's been calling from a Task method.

I also tried to get the Context like:

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

But it HttpContext.Current is returning null to me.

Dan
  • 1,518
  • 5
  • 20
  • 48
  • What does `HMTLHelperExtensions.convertStringToRouteValueDictionary(myparameters)` return? – Luke Jul 28 '15 at 15:28
  • Return a list with route values. – Dan Jul 28 '15 at 15:31
  • Obviously, but what does it actually return when it is run in your example? And what is the value of variable `myparameters`? – Luke Jul 28 '15 at 15:31
  • sorry @Coulton. The value of myparameters are "id=2". The result returns a list of keys, in this case, a Key called id, with the value 2 – Dan Jul 28 '15 at 15:38

1 Answers1

7

You need to pass the current RequestContext. Otherwise, it has no way to generate the appropriate urls for you because it's lacking the context:

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

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

See MSDN

haim770
  • 48,394
  • 7
  • 105
  • 133
  • is there a way for doing this without put the RequestContent? My class has no relationship with the controller. – Dan Jul 28 '15 at 15:34
  • 1
    You should be using this helper in the view I think... not in a controller – Luke Jul 28 '15 at 15:36
  • I suppose that we really need to know why you're generating the URL and where you're generating it to come up with a decent solution – Luke Jul 28 '15 at 15:40
  • You don't necessarily need a controller, but a request context. You can create one yourself, but you'll still need to provide the current `RouteData` somehow (as well as the `HttpContext`, but it can be easily access using `HttpContext.Current`). – haim770 Jul 28 '15 at 15:41
  • @Coulton, my intention is to use it in a classe, separated from View or Controller... to generate a link (URL) to me. Is there another way for doing it passing the name of an Action and Controller + routes? – Dan Jul 28 '15 at 15:42
  • @haim770, I've changed the code, trying to get HttpContext.Current.Request.RequestContext, but, HttpContext.Current is returning null to me. – Dan Jul 28 '15 at 15:43
  • 1
    You want to auto generate URLs according to the routes that you have set up in your MVC application, therefore it will need that information. Other than just generating a string that you know to be a correct URL for your application, how else do you expect a method within C# to know what URL to generate? – Luke Jul 28 '15 at 15:43
  • 1
    @Dan, There could be many reasons why the `HttpContext` is not available to you. But, if you're already referencing `System.Web` from your Business Layer (which is usually not a good thing), then pass the current `RequestContext` from the calling method. Otherwise, you'll have to re-think whether url generation really belongs in your Business Layer. – haim770 Jul 28 '15 at 15:55