Overriding controller action for page url



I'm trying to override the action for the url that UrlResolver returns, by using this code

    public static string GetUrl(
        this UrlResolver urlResolver,
        PageReference pageLink,
        string language,
        object routevalues)
          new VirtualPathArguments
              RouteValues = new RouteValueDictionary(routevalues)


                    new { Action = nameof(SomeController.SomeActionOtherThanIndex) })

but haven't been able to return an url that does not also include the action in the query string, that is, I end up with the relative path "/path/to/page//?Action=<>action_name>". My hack for now is to remove the query string parameter by using QueryCollection.Remove from EPiServer.UrlBuilder, but I'd much rather prevent the cause than handle the effect.

Any pointers?

May 02, 2017 19:12

The main point to consider when using MVC with EPiServer, is that in MVC a URL maps to controller and an action.


In EPiServer this isn't case. A URL will point to a page type with a specific ID.

May 03, 2017 12:54

Hi Gayathri,

Episerver also maps to controller and action; it is just a bit more hidden :) The url for a page of type T will typically be routed to the single controller that implements PageController<T>. UrlResolver will resolve to the default action for the controller, which is normall the Index action.

My challenge is to have UrlResolver resolve to an action other than the default one, without the query string argument. The url relative path for any non-default action would be the same as for the default action, with the action name as the next segment.

May 03, 2017 13:06

Without studying the EPiServer routing code in detail, one thought that pops in to my head is casing. What if you try with 'action' instead of 'Action', that is like:

                    new { action = nameof(SomeController.SomeActionOtherThanIndex) })
May 03, 2017 13:59

Hi Johan,

Thanks for the suggestion! Casing did not help, but in the process of trying it, I noticed that VirtualPathArguments has a separate property for the action. I'd managed to totally overlook it before... When setting that property, the url was resolved like I wanted :)


Per Atle

May 03, 2017 17:12
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.