A link question - MVC2 C#

One of my navigation link is dynamic, for this i have created an action which returns a string when i call it with Html.RenderAction. This is working and no problems here.

How can i call the same action from inside a method that is inside a class?

So i have:


UrlHelper helper = new UrlHelper(new RequestContext(wrapper, new RouteData()));

string url = helper.action(....)

But i can’t use helper.action here as it will point to the following action but not the result returned.


public string GetItemUrl()
        {
            string Url = String.Empty;

            if (conditionMet) 
            {
                Url = Url.Action(ActionNames.Home, ControllerNames.Count,
                       new
                       {
                           Area = AreaNames.Estimates
                       }, HTTP);
            }
            else
            {
                Url = Url.Action(ActionNames.Index, ControllerNames.Count,
                       new
                       {
                           Area = AreaNames.Estimates
                       }, HTTP);
            }

            return Url;
        }

Your problem seems to be an ‘architectural’ one. That routing logic shouldn’t be a controller action. What are you trying to accomplish? It looks like a php way of doing things which is not appropriate.

We have two associated services. Service one is always active where as service two is only active during certain part of the year.

We have only one link to these services. For this, i have this action which checks the status and if service two is active then returns a link to an intermediate page that has options for both the services. If service two is not active then it returns a direct link to service one.

You should have an object keeping the status of the server and a simple utility which generates the url . WHere you put that utility depends on from where you want to use it and I see only 2 valid places: the controller itself or a view, and both have access to the UrlHelper.

So I’d implement the utility as an extension method for UrlHelper.