本文整理汇总了C#中System.Web.HttpContextBase.SetPrincipalAppLanguageForRequest方法的典型用法代码示例。如果您正苦于以下问题:C# HttpContextBase.SetPrincipalAppLanguageForRequest方法的具体用法?C# HttpContextBase.SetPrincipalAppLanguageForRequest怎么用?C# HttpContextBase.SetPrincipalAppLanguageForRequest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Web.HttpContextBase
的用法示例。
在下文中一共展示了HttpContextBase.SetPrincipalAppLanguageForRequest方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProcessIncoming
/// <summary>
/// Implements the Early Url Localization logic.
/// <see href="https://docs.google.com/drawings/d/1cH3_PRAFHDz7N41l8Uz7hOIRGpmgaIlJe0fYSIOSZ_Y/edit?usp=sharing"/>
/// </summary>
public void ProcessIncoming(
HttpContextBase context)
{
// Is URL explicitly excluded from localization?
if (!m_urlLocalizer.FilterIncoming(context.Request.Url)) {
return; } // YES. Continue handling request.
bool allowRedirect =
context.Request.HttpMethod.Equals("GET", StringComparison.OrdinalIgnoreCase)
|| context.Request.HttpMethod.Equals("HEAD", StringComparison.OrdinalIgnoreCase);
// NO. Is request URL localized?
string urlNonlocalized;
string langtag = m_urlLocalizer.ExtractLangTagFromUrl(context, context.Request.RawUrl, UriKind.Relative, true, out urlNonlocalized);
if (langtag == null)
{
// NO.
// langtag = best match between
// 1. Inferred user languages (cookie and Accept-Language header)
// 2. App Languages.
LanguageTag lt = context.GetInferredLanguage();
// If redirection allowed...redirect user agent (browser) to localized URL.
// The principle purpose of this redirection is to ensure the browser is showing the correct URL
// in its address field.
if (allowRedirect) {
RedirectWithLanguage(context, lt.ToString(), m_urlLocalizer);
return;
}
// Otherwise, handle the request under the language infered above but without doing the redirect.
// NB: this will mean that the user agent (browser) won't have the correct URL displayed;
// however, this typically won't be an issue because we are talking about POST, PUT and DELETE methods
// here which are typically not shown to the user.
else {
context.SetPrincipalAppLanguageForRequest(lt);
return; // Continue handling request.
}
}
// YES. Does langtag EXACTLY match an App Language?
LanguageTag appLangTag = LanguageHelpers.GetMatchingAppLanguage(langtag);
if (appLangTag.IsValid()
&& appLangTag.Equals(langtag))
{
// YES. Establish langtag as the PAL for the request.
context.SetPrincipalAppLanguageForRequest(appLangTag);
// Rewrite URL for this request.
context.RewritePath(urlNonlocalized);
// Continue handling request.
return;
}
// NO. Does langtag LOOSELY match an App Language?
else if (appLangTag.IsValid()
&& !appLangTag.Equals(langtag))
{
// YES. Localize URL with matching App Language.
// Conditionally redirect user agent to localized URL.
if (allowRedirect) {
RedirectWithLanguage(context, appLangTag.ToString(), m_urlLocalizer);
return;
}
}
// NO. Do nothing to URL; expect a 404 which corresponds to language not supported.
// Continue handling request.
}