Hi Saran
You need to move your logic from the ConvertToInternalInternal method to TryConvertToInternal method.
Hope this helps
Frederik
Thanks a lot Frederik it worked well after converting my method ConvertToInternalInternal to TryConvertToInternal method.But i got another issue my language selection dropdown list in the page was not working... Refer my another method above ConvertToExternalInternal any problem with that.? the requirement is if change the language in the dropdown list language the pagelanguage should change according to the language selected in the dropdownlist..Previously it also worked well with EPiServer CMS 5.
Thanks a lot again!!!
The code below is just something I copied from the Demo tempaltes project:
<li runat="server" visible="false" class="iconLink languageLink"> <asp:HyperLink ID="Language" runat="server" Visible="false"></asp:HyperLink> <asp:Label ID="LanguageListLabel" runat="server" AssociatedControlID="LanguageList" CssClass="hidden" Visible="false" text="Other languages" /> <asp:DropDownList runat="server" ID="LanguageList" Visible="false" AutoPostBack="true" OnSelectedIndexChanged="ChangeLanguage"> <asp:ListItem Text="Other languages" Value="noLangSelected" /> </asp:DropDownList> <noscript> <asp:Button runat="server" ID="LanguageButton" OnClick="ChangeLanguage" Text="OK" Visible="false" /> </noscript> </li>
/// <summary> /// Initializes the language link. /// Checks the number of available and enabled languages. If more than two, /// populates a dropdown-menu with the available and enabled languages. /// Otherwise sets the link to the not currently active language. /// </summary> private void SetLanguage() { PageDataCollection languageBranches = DataFactory.Instance.GetLanguageBranches(CurrentPage.PageLink); //Filter so pages with Replacement language is filtered away. new FilterReplacementLanguage().Filter(languageBranches); if (languageBranches.Count > 2) { LanguageList.Visible = LanguageListLabel.Visible = LanguageButton.Visible = LanguageList.Parent.Visible = true; foreach (PageData languageBranch in languageBranches.Where(p => p.LanguageID != CurrentPage.LanguageID && LanguageBranch.Load(p.LanguageID).Enabled)) { LanguageList.Items.Add(new System.Web.UI.WebControls.ListItem(new CultureInfo(languageBranch.LanguageID).NativeName, languageBranch.LanguageID)); } } else { foreach (PageData languageBranch in languageBranches.Where(p => p.LanguageID != CurrentPage.LanguageID && LanguageBranch.Load(p.LanguageID).Enabled)) { Language.Visible = Language.Parent.Visible = true; Language.NavigateUrl = EPiServer.UriSupport.AddLanguageSelection(languageBranch.LinkURL, languageBranch.LanguageID); Language.Text = Translate(new CultureInfo(languageBranch.LanguageID).NativeName); break; } } } /// <summary> /// Redirects to the selected language. /// </summary> public void ChangeLanguage(object sender, EventArgs e) { if (LanguageList.SelectedValue != "noLangSelected") { Response.Redirect(EPiServer.UriSupport.AddLanguageSelection(CurrentPage.LinkURL, LanguageList.SelectedValue)); } }
protected override void OnLoad(System.EventArgs e) { base.OnLoad(e); if (!IsPostBack) { if (Configuration.Settings.Instance.UIShowGlobalizationUserInterface) { SetLanguage(); } } }
Hope this helps.
Frederik
Hi Frederik,
Thanks a lot for the work and spending your valuable time for me. The language also working fine now. But Bcos of my mistake in the TryConvertToInternal() method. I didn't assign preferredCulture value and after assigning like below it works...
preferredCulture = GetLanguageBranchAndPath(url.Path, out nonLocalizedPath).Culture;
Thanks a lot.. and i have marked this post as answered....
After upgrading from EPiServer CMS 5 R2 to EPiServer CMS 6 R2 im getting the page cannot be found issue for some of the pages..
Below is my episerver.config settings for URL Rewriting..
<urlRewrite defaultProvider="URLRewriter">
<providers>
<add name="EPiServerFriendlyUrlRewriteProvider" type="EPiServer.Web.FriendlyUrlRewriteProvider,EPiServer" />
<add enableSimpleAddress="true" friendlyUrlCacheAbsoluteExpiration="0:0:10" name="URLRewriter" type="EPiServer.Code.URLRewriter, EPiServer.Templates.Public" />
<add description="EPiServer identity URL rewriter" name="EPiServerIdentityUrlRewriteProvider" type="EPiServer.Web.IdentityUrlRewriteProvider,EPiServer" />
<add description="EPiServer bypass URL rewriter" name="EPiServerNullUrlRewriteProvider" type="EPiServer.Web.NullUrlRewriteProvider,EPiServer" />
</providers>
</urlRewrite>
Below is my URLRewriter.cs class file code:
using System;
using System.Collections.Specialized;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.Globalization;
using System.Text;
using System.Text.RegularExpressions;
namespace EPiServer.Code
{
public class URLRewriter : EPiServer.Web.FriendlyUrlRewriteProvider
{
private static PageData _startPage = DataFactory.Instance.GetPage(PageReference.StartPage);
private static PageReference _catalogueResultPageRef = new PageReference(Convert.ToInt32(_startPage["CatalogueResultPage"].ToString()));
private static PageReference _productDataPageRef = new PageReference(Convert.ToInt32(_startPage["ProductDataPage"].ToString()));
private string _catalogueResultPageNameInUrl, _catalogueResultPageTypeFileName, _catalogueResultUrlPrefix;
private string _productDataPageNameInUrl, _productDataPageTypeFileName, _productDataUrlPrefix;
public URLRewriter(): base()
{
//Get the EPiServer page
PageData catalogueResult = DataFactory.Instance.GetPage(_catalogueResultPageRef);
PageData productData = DataFactory.Instance.GetPage(_productDataPageRef);
//Get the URL segment of the page (the "Page name in address" property)
_catalogueResultPageNameInUrl = catalogueResult.URLSegment;
_productDataPageNameInUrl = productData.URLSegment;
//Get the physical file name (the .aspx file)
_catalogueResultPageTypeFileName = PageType.Load(catalogueResult.PageTypeID).FileName;
_productDataPageTypeFileName = PageType.Load(productData.PageTypeID).FileName;
//Get the URL prefix (for example "/Catalogue-Result/")
_catalogueResultUrlPrefix = string.Concat("/", _catalogueResultPageNameInUrl, "/");
_productDataUrlPrefix = string.Concat("/", _productDataPageNameInUrl, "/");
}
protected override bool ConvertToExternalInternal(UrlBuilder url, object internalObject, Encoding toEncoding)
{
PageData catalogueResult = DataFactory.Instance.GetPage(_catalogueResultPageRef);
PageData productData = DataFactory.Instance.GetPage(_productDataPageRef);
//Get the URL segment of the page (the "Page name in address" property)
_catalogueResultPageNameInUrl = catalogueResult.URLSegment;
_productDataPageNameInUrl = productData.URLSegment;
//Get the physical file name (the .aspx file)
_catalogueResultPageTypeFileName = PageType.Load(catalogueResult.PageTypeID).FileName;
_productDataPageTypeFileName = PageType.Load(productData.PageTypeID).FileName;
//Get the URL prefix (for example "/Catalogue-Result/")
_catalogueResultUrlPrefix = string.Concat("/", _catalogueResultPageNameInUrl, "/");
_productDataUrlPrefix = string.Concat("/", _productDataPageNameInUrl, "/");
//Check the internal URL to see if a user //account page is being requested
if (url.Path.ToLower().Contains(_catalogueResultPageTypeFileName.ToLower()) || _catalogueResultPageTypeFileName.ToLower().Contains(url.Path.ToLower()) && url.Path != string.Empty)
{
//Create the custom friendly URL
string friendlyPath = string.Empty;
if (url.QueryCollection["epslanguage"] != null)
{
if (url.QueryCollection["epslanguage"].ToString() != string.Empty && url.QueryCollection["epslanguage"] != ContentLanguage.PreferredCulture.Name)
{
friendlyPath = string.Concat("/", url.QueryCollection["epslanguage"], "/", _catalogueResultPageNameInUrl, "/");
}
else
friendlyPath = string.Concat("/", LanguageBranch.Load(ContentLanguage.PreferredCulture).LanguageID, "/", _catalogueResultPageNameInUrl, "/");
}
else
friendlyPath = string.Concat("/", LanguageBranch.Load(ContentLanguage.PreferredCulture).LanguageID, "/", _catalogueResultPageNameInUrl, "/");
string _keyword = string.Empty;
//Get the keyword from the querystring
if (url.QueryCollection["Tab"] != null)
{
_keyword = url.QueryCollection["Tab"];
if (_keyword != string.Empty && _keyword != null)
friendlyPath = string.Concat(friendlyPath, Url.Decode(_keyword));
}
if (url.QueryCollection["search"] != null)
{
_keyword = url.QueryCollection["search"];
if (_keyword != string.Empty && _keyword != null)
friendlyPath = string.Concat(friendlyPath, Url.Decode(_keyword).Replace(" ", "-"), "/");
}
//Remove querystring parameters
url.QueryCollection.Clear();
//Set the URL to the new friendly URL
url.Path = friendlyPath;
//Indicate that the original URL has been rewritten
return true;
}
else if (url.Path.ToLower().Contains(_productDataPageTypeFileName.ToLower()))
{
if (url.QueryCollection["partno"] != null)
{
//Get the keyword from the querystring
string _keyword = url.QueryCollection["partno"];
//Create the custom friendly URL
string friendlyPath = string.Empty;
if (url.QueryCollection["epslanguage"] != null)
{
if (url.QueryCollection["epslanguage"].ToString() != string.Empty && url.QueryCollection["epslanguage"] != ContentLanguage.PreferredCulture.Name)
{
friendlyPath = string.Concat("/", url.QueryCollection["epslanguage"], "/", _productDataPageNameInUrl, "/");
}
else
friendlyPath = string.Concat("/", LanguageBranch.Load(ContentLanguage.PreferredCulture).LanguageID, "/", _productDataPageNameInUrl, "/");
}
else
friendlyPath = string.Concat("/", LanguageBranch.Load(ContentLanguage.PreferredCulture).LanguageID, "/", _productDataPageNameInUrl, "/");
if (_keyword != string.Empty && _keyword != null)
friendlyPath = string.Concat(friendlyPath, Url.Decode(_keyword).Replace(" ", "-"), "/");
//Remove querystring parameters
url.QueryCollection.Clear();
//Set the URL to the new friendly URL
url.Path = friendlyPath;
//Indicate that the original URL has been rewritten
return true;
}
else
{
//Use the default rewriting behavior
return base.ConvertToExternalInternal(url, internalObject, toEncoding);
}
}
else //Not a user account details page
{
//Use the default rewriting behavior
return base.ConvertToExternalInternal(url, internalObject, toEncoding);
}
}
protected override bool ConvertToInternalInternal(UrlBuilder url, ref object internalObject)
{
PageData catalogueResult = DataFactory.Instance.GetPage(_catalogueResultPageRef);
PageData productData = DataFactory.Instance.GetPage(_productDataPageRef);
//Get the URL segment of the page (the "Page name in address" property)
_catalogueResultPageNameInUrl = catalogueResult.URLSegment;
_productDataPageNameInUrl = productData.URLSegment;
//Get the physical file name (the .aspx file)
_catalogueResultPageTypeFileName = PageType.Load(catalogueResult.PageTypeID).FileName;
_productDataPageTypeFileName = PageType.Load(productData.PageTypeID).FileName;
//Get the URL prefix (for example "/Catalogue-Result/")
_catalogueResultUrlPrefix = string.Concat("/", _catalogueResultPageNameInUrl, "/");
_productDataUrlPrefix = string.Concat("/", _productDataPageNameInUrl, "/");
if (url == null)
{
return false;
}
bool _bReplaceAP = false;
//Check if the requested URL should be handled by the custom URL rewriting
//if ((url.Path.StartsWith(_catalogueResultUrlPrefix, StringComparison.OrdinalIgnoreCase)))
if (url.Path.ToLower().Contains(_catalogueResultUrlPrefix.ToLower()))
{
NameValueCollection queryCollection = url.QueryCollection;
//Get the language branch based on the requested URL
string nonLocalizedPath;
ContentLanguage.PreferredCulture = GetLanguageBranchAndPath(url.Path, out nonLocalizedPath).Culture;
if (url.Path.ToUpper().Contains("/AP") && ContentLanguage.PreferredCulture.Name.Trim().ToUpper() == "EN-AU")
_bReplaceAP = true;
//Set the internal object to a PageReference
internalObject = _catalogueResultPageRef;
//Ensure that the URL has a trailing slash
if (!url.Path.EndsWith("/"))
{
url.Path = url.Path + "/";
//HttpContext.Current.Response.Redirect((string)url);
}
string friendlyUrl = url.Path;
//Get the internal URL
string internalUrl = new Url(DataFactory.Instance.GetPage(_catalogueResultPageRef).StaticLinkURL).Path;
//Set the URL to the internal URL
url.Path = internalUrl;
//Extract the URL segment of the partno
string urlSegment = friendlyUrl.ToUpper().Replace(_catalogueResultUrlPrefix.ToUpper(), string.Empty);
urlSegment = urlSegment.ToUpper().Replace("/" + ContentLanguage.PreferredCulture.Name.ToUpper(), string.Empty);
urlSegment = urlSegment.ToUpper().Replace("/" + ContentLanguage.PreferredCulture.TwoLetterISOLanguageName.ToUpper(), string.Empty);
if (_bReplaceAP)
urlSegment = urlSegment.ToUpper().Replace("/AP", string.Empty);
//Remove trailing slash
urlSegment = urlSegment.TrimStart('/');
urlSegment = urlSegment.TrimEnd('/');
//urlSegment = urlSegment.Length > 1? urlSegment.Substring(0, urlSegment.Length - 1) : (urlSegment.Length == 1? (urlSegment == "/"? string.Empty: urlSegment ): urlSegment);
//Add the part no to the querystring
//of the internal URL
if (urlSegment != string.Empty)
{
if(urlSegment.Length == 1)
queryCollection.Add("Tab", Url.Decode(urlSegment).Replace(" ", "-"));
else
queryCollection.Add("search", Url.Decode(urlSegment).Replace(" ", "-"));
}
//Add additional querystring parameters
//to the internal URL
//queryCollection.Add("epslanguage", ContentLanguage.PreferredCulture.TwoLetterISOLanguageName);
queryCollection.Add("epslanguage", ContentLanguage.PreferredCulture.Name);
queryCollection.Add("id", _catalogueResultPageRef.ID.ToString());
//Indicate URL rewriting
return true;
}
//else if ((url.Path.StartsWith(_productDataUrlPrefix, StringComparison.OrdinalIgnoreCase)))
else if (url.Path.ToLower().Contains(_productDataUrlPrefix.ToLower()))
{
NameValueCollection queryCollection = url.QueryCollection;
//Get the language branch based on the requested URL
string nonLocalizedPath;
ContentLanguage.PreferredCulture = GetLanguageBranchAndPath(url.Path, out nonLocalizedPath).Culture;
if (url.Path.ToUpper().Contains("/AP") && ContentLanguage.PreferredCulture.Name.Trim().ToUpper() == "EN-AU")
_bReplaceAP = true;
//Set the internal object to a PageReference
internalObject = _productDataPageRef;
//Ensure that the URL has a trailing slash
if (!url.Path.EndsWith("/"))
{
url.Path = url.Path + "/";
//HttpContext.Current.Response.Redirect((string)url);
}
string friendlyUrl = url.Path;
//Get the internal URL
string internalUrl = new Url(DataFactory.Instance.GetPage(_productDataPageRef).StaticLinkURL).Path;
//Set the URL to the internal URL
url.Path = internalUrl;
//Extract the URL segment of the partno
string urlSegment = friendlyUrl.ToUpper().Replace(_productDataUrlPrefix.ToUpper(), string.Empty);
urlSegment = urlSegment.ToUpper().Replace("/" + ContentLanguage.PreferredCulture.Name.ToUpper(), string.Empty);
urlSegment = urlSegment.ToUpper().Replace("/" + ContentLanguage.PreferredCulture.TwoLetterISOLanguageName.ToUpper(), string.Empty);
if (_bReplaceAP)
urlSegment = urlSegment.ToUpper().Replace("/AP", string.Empty);
//Remove trailing slash
urlSegment = urlSegment.TrimStart('/');
urlSegment = urlSegment.TrimEnd('/');
//urlSegment = urlSegment.Length > 1 ? urlSegment.Substring(0, urlSegment.Length - 1) : urlSegment;
//urlSegment = urlSegment.Length > 1 ? urlSegment.Substring(0, urlSegment.Length - 1) : (urlSegment.Length == 1 ? (urlSegment == "/" ? string.Empty : urlSegment) : urlSegment);
//Add the part no to the querystring
//of the internal URL
if (urlSegment != string.Empty)
queryCollection.Add("partno", Url.Decode(urlSegment).Replace(" ", "-"));
//Add additional querystring parameters
//to the internal URL
//queryCollection.Add("epslanguage", ContentLanguage.PreferredCulture.TwoLetterISOLanguageName);
queryCollection.Add("epslanguage", ContentLanguage.PreferredCulture.Name);
queryCollection.Add("id", _productDataPageRef.ID.ToString());
//Indicate URL rewriting
return true;
}
else
{
//Use the default rewriting behavior
return base.ConvertToInternalInternal(url, ref internalObject);
}
}
public override bool ConvertToInternal(UrlBuilder url, out object internalObject)
{
PageData catalogueResult = DataFactory.Instance.GetPage(_catalogueResultPageRef);
PageData productData = DataFactory.Instance.GetPage(_productDataPageRef);
//Get the URL segment of the page (the "Page name in address" property)
_catalogueResultPageNameInUrl = catalogueResult.URLSegment;
_productDataPageNameInUrl = productData.URLSegment;
//Get the physical file name (the .aspx file)
_catalogueResultPageTypeFileName = PageType.Load(catalogueResult.PageTypeID).FileName;
_productDataPageTypeFileName = PageType.Load(productData.PageTypeID).FileName;
//Get the URL prefix (for example "/Catalogue-Result/")
_catalogueResultUrlPrefix = string.Concat("/", _catalogueResultPageNameInUrl, "/");
_productDataUrlPrefix = string.Concat("/", _productDataPageNameInUrl, "/");
internalObject = null;
//Check if a user account details page is being requested
//if ((url.Path.StartsWith(_catalogueResultUrlPrefix, StringComparison.OrdinalIgnoreCase)) || (url.Path.StartsWith(_productDataUrlPrefix, StringComparison.OrdinalIgnoreCase)))
if ((url.Path.ToLower().Contains(_catalogueResultUrlPrefix.ToLower())) || (url.Path.ToLower().Contains(_productDataUrlPrefix.ToLower())))
{
ConvertToInternalInternal(url, ref internalObject);
return true;
}
else //Not a user account details page, use default rewriting
{
return base.ConvertToInternal(url, out internalObject);
}
}
}
}