Sitefinity 404多语言页面

时间:2018-06-15 17:01:18

标签: c# asp.net .net model-view-controller sitefinity

所以我有一个带有自定义错误页面的Sitefinity实例,但由于某种原因,它无法正确呈现正确的页面语言。它总是默认为主要的(英语),当用户在法国版本的网站上时它应该呈现法语页面。

现在......我已经关注了这个Sitefinity Doc,但它仍然无法正常工作。

web.config中的My HTTPErrors部分

<httpErrors existingResponse="Replace" errorMode="Custom">
  <clear />
  <remove statusCode="403" subStatusCode="-1" />
  <remove statusCode="404" subStatusCode="-1" />
  <remove statusCode="500" subStatusCode="-1" />
  <error statusCode="403" prefixLanguageFilePath="" path="/error" responseMode="ExecuteURL" />
  <error statusCode="404" prefixLanguageFilePath="" path="/error" responseMode="ExecuteURL" />
  <error statusCode="500" prefixLanguageFilePath="" path="/error" responseMode="ExecuteURL" />
</httpErrors>
private readonly string errorPageName = "Error";

protected void Application_Error(object sender, EventArgs e)
{
    if (HttpContext.Current.Request.Url.PathAndQuery.Contains("Error")) // URL of 404 page
    {
        return;
    }

    var ex = (HttpException)this.Server.GetLastError();
    int httpCode = ex.GetHttpCode();

    if (httpCode == 404)
    {
        //this.Handle404();
        this.Handle404WithSitefinityPage(this.errorPageName);
    }
}

protected void Application_EndRequest(object sender, EventArgs e)
{
    if (HttpContext.Current.Request.Url.PathAndQuery.Contains("Error")) // URL of 404 page
    {
        return;
    }

    HttpResponse response = HttpContext.Current.Response;
    int httpCode = response.StatusCode;

    if (httpCode == 404)
    {
        this.Handle404WithSitefinityPage(this.errorPageName);
        //this.Handle404();
    }
}

/// <summary>
/// Handles error for both multisite and singlesite modes regardles of culture
/// This methods gets a Sitefinity page and renders it's content without rewriting the current 
/// URL, does not issue a 301/302 redirect to the acutal 404 (the actual 404 page will return http 200 code)
///  </summary>
/// <param name="sitefinity404PageTitle"></param>
protected void Handle404WithSitefinityPage(string sitefinity404PageTitle)
{
    this.Server.ClearError();

    PageManager manager = PageManager.GetManager();

    // This call will get the correct page in multisite and multinglual mode
    PageNode pageNode = manager
        .GetPageNodes()
        .First(x => x.Title.Contains(sitefinity404PageTitle));

    PageData pd = pageNode.GetPageData(Thread.CurrentThread.CurrentCulture);
    pageNode = pd.NavigationNode;

    this.Response.StatusCode = 404;

    // The InMemoryPageRender has some limitations, it does not render navigations 
    // widgets, it does not aways include JS that come from widgets
    // Error pages created to be used by this logic must be plain and not have
    // heavy JS dependencies
    string content = new InMemoryPageRender().RenderPage(pageNode);
    this.Response.Write(content);

    this.Response.End();
}

0 个答案:

没有答案