Don't miss out Virtual Happy Hour today (April 26).

Try our conversational search powered by Generative AI!

Overriding controller action for page url

Vote:
 

Hi,

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)
    {
      return
        urlResolver.GetUrl(
          pageLink,
          language,
          new VirtualPathArguments
            {
              RouteValues = new RouteValueDictionary(routevalues)
            });
    }

and

UrlResolver.Current.GetUrl(
                    somePage.PageLink,
                    null,
                    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?

#178139
May 02, 2017 19:12
Vote:
 

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.

#178164
May 03, 2017 12:54
Vote:
 

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.

#178165
May 03, 2017 13:06
Vote:
 

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:

UrlResolver.Current.GetUrl(
                    somePage.PageLink,
                    null,
                    new { action = nameof(SomeController.SomeActionOtherThanIndex) })
#178167
May 03, 2017 13:59
Vote:
 

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 :)

Cheers,

Per Atle

#178180
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.