November Happy Hour will be moved to Thursday December 5th.

Multi Ligual site causing an exception when looking at previous versions of pages

Vote:
 
We have a multi lingual site. The project manager is VERY SEO conscious and requested that we make sure that if you entered the site with a URL without a language defined it would redirect to the page for your default language,
i.e. www.test.com/page1 would redirect to www.test.com/en/page1.

To do this I added the following code into the PreInit event handler of template page:
if (this.CurrentPage != null)
{
string language = this.CurrentPage.LanguageID;
string pageLinkRUL = this.CurrentPage.LinkURL;
if (!string.IsNullOrEmpty(
ConfigurationManager.AppSettings["DispableArabic"]))
{
if (ConfigurationManager.AppSettings["DispableArabic"].ToLower() == "true"
&& language == "ar")
{
language = "en";
pageLinkRUL = pageLinkRUL.Replace("=ar", "=en");
}
}
if (!Request.RawUrl.StartsWith("/" + language))
{
Response.Clear();
Response.StatusCode = 301;
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", pageLinkRUL);
Response.End();
}
}



This code works fine except when you are looking at previous versions of page. If you go into the Version List tab and select a previous version of the page and then press the View tab you get the following exception:

[NullReferenceException: Object reference not set to an instance of an object.]

EPiServer.Web.PageExtensions.ContextMenu.SetupOnPageEdit() +88
EPiServer.Web.PageExtensions.ContextMenu.RegisterMenuScripts(Object sender, EventArgs e) +119
System.EventHandler.Invoke(Object sender, EventArgs e) +0
System.Web.UI.Control.OnInit(EventArgs e) +99
System.Web.UI.Page.OnInit(EventArgs e) +9
EPiServer.PageBase.OnInit(EventArgs e) +316
System.Web.UI.Control.InitRecursive(Control namingContainer) +321
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +834

When I first did some investigation I noticed that the CurrentPage property of the page was not being populated, so I had to add some code in various places to make sure it would not throw exceptions.

I suspect that the above exception is due to the same issue, however I'm not sure how to get around it? Am I doing the right thing in the redirection code, or should I do it in a different way and that would not cause this issue?

Any ideas or suggestions would be gratefully received.

Jim

 

#21655
Jul 03, 2008 17:31
Vote:
 

Hello Jim,

Sound like somthing this faq might take care of.

 

#21656
Jul 03, 2008 17:39
Vote:
 
Thanks for the response Petter, but unfortunately it did not work.

I forgot to say that we are using version 5.1.422.122 (which is SP1 I think).

Any other ideas would be great....

Jim.
#21679
Jul 04, 2008 16:59
Vote:
 

I would not do this on the page but rewrite the url before it reaches the page.

A (untested) suggestion is to add your own event handler to Global.UrlRewriteProvider.ConvertedToInternal and look at e.Url. If you want to change language you can either issue a response redirect with abort or modify the query paramter epslanguage to show another language.

If the parameter idkeep is present do not do anything because this is a preview request for edit mode.

/Fredrik

#21729
Jul 07, 2008 14:14
Vote:
 

Thanks for the ideas Fredrik,

I had a look at this, but I don't have access to the information I need at the point when the ConvertedToInternal event fires. The Response object is not available, and therefore I cannot tell what language the user expects to see.

I did modify my previous code from

if (this.CurrentPage != null)

to

if (Request.RawUrl.ToLower().IndexOf("idkeep=true") == -1)

to stop it redirecting when in preview mode. The trouble is that this didn't fix the issue. I have now removed the redirect code and the issue still occours. I've used Reflector and it looks like it's because the CurrentPage value is null. I assume this is usually the case, so am not sure why it's causing this problem.

Any ideas on the cause?

Jim. 

 

 

#21908
Jul 14, 2008 15:49
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* 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.