0

There are some methods like the followings in UrlHelper class in MVC4

    public string Action(string actionName, object routeValues);

    public string Action(string actionName, RouteValueDictionary routeValues);

...but i want to change route values in every Action method calls for this I wrote CustomUrlHelper class like the following:

public class CustomUrl : UrlHelper
{  
    public CustomUrl()
        : base(HttpContext.Current.Request.RequestContext,RouteTable.Routes){}


    public string CustomAction(string actionName, string controllerName, string areaName, object routeValues, bool generateToken)
    { 
        RouteValueDictionary rvd = new RouteValueDictionary(routeValues);
        //some changes on route value dictionary


        return Action(actionName,controllerName, rvd);
    }


}

If we know action name controller name than every thing is OK, but if we just know a base URL and route value dictionary than my CustomAction method does not work.

Let's say if I have just a linkbase like A/B/C/D and RouteValueDictionary.

Now I want a method it gets baseUrl and RouteValueDictionary and produces complete link.

martoncsukas
  • 2,077
  • 20
  • 23
brtb
  • 2,201
  • 6
  • 34
  • 53
  • Not really understanding what you are trying to achieve. I think that the kind of thing you are trying to do should be done in the Routes collection... – Romias Oct 20 '14 at 22:55
  • as you can see my CustomAction function, if we know action name, controller name etc than i can easily build a link and in it i can do RouteValueDictionary whatever i want. But if we just know a link and try to add route values to end of it than my method doesnt work anymore. what should i do? – brtb Oct 21 '14 at 06:50
  • What you mean by _"just know a link"_? If you mean you have a string such as `var string = "SomeController/SomeAction/SomeParameter";` then you can create a [Uri](http://msdn.microsoft.com/en-us/library/system.uri(v=vs.110).aspx) and use its properties to extract segements –  Oct 22 '14 at 01:03
  • is it possible to extract all segments? what if i have an area in my project then what happens? can you please show me an example – brtb Oct 23 '14 at 06:23
  • @bttb, If you were to create a `Uri` based on this page then its properties would include `scheme="http"`, `host="stackoverflow.com"` and `segments=["questions/", "26444706/", "how-to-build-custom-url-link-mvc"]`. I'm not really sure what you trying to do and in what context so I'm not sure if this is what you need. (p.s. when adding your next comment, click help on the right so you know how to notify a user) –  Oct 24 '14 at 07:25

0 Answers0