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.
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
Comments
Petter Klang 2 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.
Arild Moland 1 years ago
I'm getting this exception on our website on some, but not all, PageTypeBuilder-enabled pages when these are accessed through SSL. We're using CMS 5 R2SP2. I first noticed that I got this exception if the website name in the EPiServer config section of web.config didn't match the domain in the URL, but I fail miserably at understanding what my cause it to happen when browsing a page using https, rather than just http. Especially since it does not happen for all pages using PageTypeBuilder.
Any ideas?
Joel Abrahamsson 1 years ago
Unfortunately I don't have any good advice for you. Haven't experienced such problems myself.
Are you experiencing it on any non-PTB pages? Or rather, have you done any such step to isolate the problem to PTB?
If you have isolated it to only PTB-pages, have you tried the above fix. Not sure what that would do but it could be worth a try I guess :)
Arild Moland 1 years ago
Thank you for the fast reply!
The error does not occur on non-PTB pages. I haven't tried the above fix, but come to think of it, the error only seems to occur on PTB-pages where we do a bit of custom URL rewriting. Other PTB-pages which are accessed through the standard, EPi-rewritten URLs, work just fine under SSL. Hmmm... custom URL-rewriting is a drag.
Well, guess I got a lead on where to investigate, after all :)
Ranjit J. Vaity 1 years ago
I tried using code from http://world.episerver.com/Articles/Items/Introducing-Page-Type-Builder/
but I get the error "The current page is not of type MyPageType. This often occurs when no id query string parameter is specified.
"
then I followed your instructions but error still persists.
Please help.
Thanks,
Ranjit J. Vaity
Joel Abrahamsson 1 years ago
Hi Ranjit,
Could you give a little more detail? Has the page type been generated by Page Type Builder or have you create class that maps to an existing page type?
Ranjit J. Vaity 1 years ago
Thank you for your quick reply.
I did follow every step that has been provided in the article.
1. I created a class MyPageType inheriting it from TypedData attributing it as [PageType(Filename= "~/Templates/Public/Pages/HelloWorld.aspx")]
2. A virtual string property was created MainBody attributing it as [PageTypeProperty]
3. In .aspx I have <%= CurrentPage.MainBody %>
4. Compiled sucessfully, Created a Page type using this template added XHTML Property called MainBody and in edit mode created a page add sample text to the rich text editor.
But when I view this page it give me the above mentioned error. Finally I made changes to Application_Start in Global.asax.cs file for URL rewriting. But error persists.
Please guide me if I am missing anything.
Thanks again.
Regards,
Ranjit J. Vaity
Joel Abrahamsson 1 years ago
What is the URL to the page that you are trying to view?
Ranjit J. Vaity 1 years ago
Hello Joel,
Following is the URL when I try to view directly from browser:
http://episerver.sandbox/en/HW/
Thank you! :)
Regards,
Ranjit J. Vaity
Joel Abrahamsson 1 years ago
Very weird. You are welcome to send your code to mail@joelabrahamsson.com if you want me to take a look.
Dan Jansson 7 months ago
I ran into this problem when I did some re-arrangement among the page types in my clients web site using EPiServer Admin Mode. I think that the problem was that I renamed the page types that had been generated by PTB, which made PTB re-generate the "missing" page types. I also converted some pages to match the renamed page types which probably can explain why I got a missmatch in the page types.
Solution: Control all page type naming from PTB and be careful when renaming page types in web sites that uses PTB.
There is also an option that make it possible to disable the automatic generating of changes using PTB. There is a blog entry somewhere on Joels blog that covers this.
Jon Haakon Ariansen 7 months ago
Updating all dependend component and main project to the latest version of Episerver (CMS 6 R2) and Pagetypebuilder (1.3.1) worked for me.