如何:自定义变体根登录逻辑

上次修改时间: 2010年11月12日

适用范围: SharePoint Server 2010

在 Microsoft SharePoint Server 2010 中设置网站集和配置变体时,它会执行以下操作:

  • 使用母版页和页面布局库中的 VariationRootPageLayout.aspx 页面布局文件在变体主网站中创建一个名为 VariationRoot 的页。

  • 使用该 VariationRoot 页替换变体主网站的默认欢迎页面。

如果您浏览到变体主网站,则 SharePoint Server 2010 会查找该网站的 WelcomePage 属性,并运行 VariationRoot 页。因为该页基于 VariationRootPageLayout.aspx,所以它会运行页面布局文件。VariationRootPageLayout 页面布局包含对名为 VariationsRootLanding.ascx 的文件的引用和一个在 VariationsRootLanding.ascx 文件中定义的用户控件。页面布局执行 VariationsRootLanding.ascx 文件中的逻辑,并将用户重定向到某个变体网站。

VariationsRootLanding.ascx 文件中使用的默认逻辑是将用户重定向到与浏览器的 accept lang 或当前文档使用的语言代码数组相匹配的变体网站。VariationsRootLanding.ascx 文件在以下路径中定义:\<Program Files>\Common Files\Microsoft Shared Debug\Web Server Extensions\14\Template\ControlTemplates。

如果希望默认变体根登录页面逻辑将用户重定向到不基于 accept lang 的变体网站,您可以使用以下三种方法之一实现此目的。每个选项均有自己的优缺点,在决定使用哪种方法之前应认真考虑。

  • 直接编辑 VariationsRootLanding.ascx 文件。

    这是编辑逻辑的最快捷的方法。只需根据需要在前端 Web 服务器文件系统上直接编辑 VariationsRootLanding.ascx 文件中的逻辑。

    备注

    您必须更新每台前端 Web 服务器计算机上的 VariationsRootLanding.ascx 文件。服务器场中的所有网站集均绑定到了您指定的自定义变体根登录逻辑。

  • 将 VariationsRootLanding.ascx 文件的内容复制到母版页和页面布局库中的页面布局中。

    有关实现此方法的详细信息,请参阅第一个过程:将 VariationsRootLanding.ascx 文件的内容复制到页面布局中。与前面的方法类似,此方法也通过直接编辑来提供快速自定义,而不会出现 DLL 编译和部署问题。此外,由于每个网站集都使用自己的页面布局集,因此此方法还允许在网站集级别自定义变体根登录逻辑。若要完成该过程,您必须更新每个前端 Web 服务器计算机上的 web.config 文件。

    警告注释警告

    当在 VariationRootPageLayout.aspx 文件上设置 AllowCompilation 标志时需谨慎。使用内嵌代码意味着某些人可将恶意代码注入到页中。

  • 创建预编译程序集。

    有关实现此方法的详细信息,请参阅第二个过程:创建预编译程序集。此方法不会带来与打开母版页和页面布局库项以执行内嵌代码相关的安全风险。与前面的方法类似,将在网站集级别进行自定义设置。但是,每当您需要更改变体根登录逻辑时,您必须重新编译程序集。为此,此方法提供了速度较慢的更改-测试-部署循环。您必须将程序集部署到所有前端 Web 服务器文件系统计算机上。

还为您提供了另一种方案,即修改 VariationsRootLanding.ascx 文件中的代码。如果您使用范围部署作业来部署变体网站,并且该作业未从部署源部署变体层次结构中的所有变体标签,则当导航到部署目标上的变体根时,通常重定向到这些标签的用户会收到 404 错误。例如,如果变体层次结构中有一个面向德语区域设置的标签,并且该标签未包含在范围部署中,则当导航到目标计算机上的变体根时,设置为首选德语区域设置的浏览器会遇到 404 错误。默认情况下,根登录逻辑不检查所使用的重定向 URL 的有效性。

解决此问题的一个方法是将根登录逻辑修改为检测目标重定向 URL 上何时有运行的网站。若要了解如何执行此操作,请参阅第三个步骤,即"将根登录逻辑修改为检测何时有运行的网站"。

将 VariationsRootLanding.ascx 文件的内容复制到页面布局中

  1. 从 VariationsRootLanding.ascx 文件中复制代码,并将其嵌入到母版页和页面布局库中的 VariationRootPageLayout.aspx 中。

  2. 在 VariationRootPageLayout.aspx 文件中进行所需更改。

  3. 从 VariationRootPageLayout.aspx 内移除对 VariationsRootLanding.ascx 及其用户控件的引用。

  4. 若要允许 VariationRootPageLayout.aspx 的内嵌代码执行,请修改每个 Web 客户端上的 web.config 文件,如下所示。

      <SharePoint>
        <SafeMode ... >
          <!-- Marks VariationRootPageLayout.aspx for ASP.NET compilation. -->
          <PageParserPath VirtualPath= "/_catalogs/masterpage/VariationRootPageLayout.aspx" 
            CompilationMode="Always"  
            AllowServerSideScript="False"
            AllowUnsafeControls="False" 
            IncludeSubFolders="True"/>
          </PageParserPaths>
        </SafeMode>
      </SharePoint>
    

创建预编译程序集

  1. 从 VariationsRootLanding.ascx 文件中复制代码,以在您自己的程序集 DLL 上创建您自己的控件。

  2. 在控件中进行所需更改。

  3. 将一个对您的控件的引用添加到母版页和页面布局库中的 VariationRootPageLayout.aspx 中。

  4. 从 VariationRootPageLayout.aspx 内移除对 VariationsRootLanding.ascx 及其用户控件的引用。

将根登录逻辑修改为检测何时有运行的网站

  1. 打开 VariationsRootLanding.ascx 文件。

  2. 在 GetRedirectUrl() 部分中,查找以下代码行。

    return (string.IsNullOrEmpty(matchedUrl) ? sourceLabelUrl : matchedUrl);
    
  3. 用以下代码替换步骤 2 中的代码行。

    // Customization for handling matchedUrl not valid. 
    // (For example, a content deployed target site collection, 
    // without source hierarchy.)
    matchedUrl = (string.IsNullOrEmpty(matchedUrl) ? sourceLabelUrl : matchedUrl);                           
    using (SPSite site = new SPSite(matchedUrl))
    using (SPWeb web = site.OpenWeb())
         {
         // If matchedUrl is the same as the URL of the Web
         // that you just opened, then matchedUrl is valid.
         if (string.Compare(matchedUrl, web.Url, StringComparison.OrdinalIgnoreCase) == 0 && web.DoesUserHavePermissions(SPBasePermissions.Open))
           {
          //Target URL is valid; return it.
            return matchedUrl;
            }
            else
            {
          //Target URL was NOT valid; the variation label is missing.
          //Perform logic here to redirect the user appropriately.
          //If nothing is done here, then this function returns null
          //and the landing behavior reverts to the
          //VariationsRootLandingRunTime control.
          //This control displays a simple error message that
          //indicates that it could not find an appropriate subsite to 
          //redirect to.
            }
       }
    

请参阅

其他资源

变体和多语言网站