UrlHelper.Action throws ArgumentNullException
Asked Answered
V

1

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.

Videlicet answered 28/7, 2015 at 15:23 Comment(4)
What does HMTLHelperExtensions.convertStringToRouteValueDictionary(myparameters) return?Berstine
Return a list with route values.Videlicet
Obviously, but what does it actually return when it is run in your example? And what is the value of variable myparameters?Berstine
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 2Videlicet
C
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

Chloride answered 28/7, 2015 at 15:27 Comment(8)
is there a way for doing this without put the RequestContent? My class has no relationship with the controller.Videlicet
You should be using this helper in the view I think... not in a controllerBerstine
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 solutionBerstine
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).Chloride
@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?Videlicet
@haim770, I've changed the code, trying to get HttpContext.Current.Request.RequestContext, but, HttpContext.Current is returning null to me.Videlicet
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?Berstine
@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.Chloride

© 2022 - 2024 — McMap. All rights reserved.