“The current page is not of type MyPageType” exception when browsing with non-friendly URL

Yesterday we ran into a problem in the project that I’m currently working on. We’re using Page Type Builder and when we tried to view a page by going to it’s non-friendly URL, that is /HelloWorld.aspx?id=30 instead of /HelloWorld/, we got an exception thrown by Page Type Builder saying “The current page is not of type <PageTypeName>”.

An exception with that error message is thrown when using TemplatePage<PageType>, UserControlBase<PageType> or one of the other base classes for pages and controls in Page Type Builder and you are trying to access a page that isn’t of the specified page type. The problem in this case was that the page with the specified ID in the query string was of the correct page type.

It turns out that EPiServers friendly URL rewriter, for some to me mysterious reason, strips the ID-parameter from the query string collection even if it doesn’t do any actual rewriting. That in turn means that when someone tries to access PageBase.CurrentPageLink, which Page Type Builders above mentioned base classes does, the property will return PageReference.StartPage (which is the default) instead of the actually requested page. And since the start page isn’t of the requested type the exception is thrown.

Luckily we found simple a workaround posted in this thread by Tiberiu Covaci. The solution is to hook into the ConvertingToInternal event in the URL rewrite provider and cancel URL rewriting if the requested URL is for an ASPX-page. Modify your global.asax.cs to contain this:

protected void Application_Start(Object sender, EventArgs e)
{
    UrlRewriteProvider.ConvertingToInternal += UrlRewriteProvider_ConvertingToInternal;
}

void UrlRewriteProvider_ConvertingToInternal(object sender, EPiServer.Web.UrlRewriteEventArgs e)
{
    e.Cancel = e.Url.Path.EndsWith(".aspx");
}

PS. For updates about new posts, sites I find useful and the occasional rant you can follow me on Twitter. You are also most welcome to subscribe to the RSS-feed.

Comments

  1. Petter Klang's avatar

    Petter Klang 1 years ago

    I think the stripping of Id's are done because if your site is using the friendly URL rewriter we do not want people being able to guess pages by passing in query strings to the site.

Add a comment

Allowed tags: <b>, <em>, <quote cite="">, <code>, <c-sharp-code>, <css-code>, <sql-code>, <xml-code>, <javascript-code>. If you want to display code examples, please remember to write &lt; for < and &gt; for >.

Follow me on Twitter

  1. Bookmarked: Google Scribe http://bit.ly/9iDp8s 2 days ago
  2. Blogged: The future of EPiMVC http://bit.ly/ck6EPg #episerver 2 days ago
  3. At Skånegläntan, as always #parentalleave 2 days ago
follow me

Latest comments

  1. Svante wrote "Yes, I noticed that it was a singleton, and I guess the real..." on Something to beware of when using EPiAbstractions and an IoC container
  2. Joel Abrahamsson wrote "Well, first of all you wont get any arguments from me regard..." on Something to beware of when using EPiAbstractions and an IoC container
  3. Svante wrote "Hmm... Since the issue really is with the public instance co..." on Something to beware of when using EPiAbstractions and an IoC container

About this site

This blog is built with EPiServer Community, EPiServer CMS, ASP.NET MVC and a bunch of other great products. The source code is available for download at the projects page, where you also can read more about this site and my other projects.

read more