国际版的NerdDinner

时间:2013-02-16 08:25:51

标签: internationalization nerddinner

您好我一直在使用国际版的: ASP.NET MVC 3国际化 - 第2部分(NerdDinner) 在: http://afana.me/post/aspnet-mvc-internationalization-part-2.aspx

不幸的是,这包含一个主要错误。 问题是我已下载代码/项目并进行构建并将其发布到本地文件夹(有一些小错误需要修复才能进行发布),然后将文件上传到我的webhotel。主页似乎工作正常,但后来我查看了Google Web Master Tool并在网站上进行了抓取,发现网站中没有一个页面可以索引,即使googles crawler也无法获取根节点: / p>

这是我在尝试获取根目录(www.thehomepage.com)或网站中的任何页面时遇到的错误:

页面无法访问

HTTP / 1.1 500内部服务器错误

    [NullReferenceException: Object reference not set to an instance of an object.]
       NerdDinner.Controllers.BaseController.ExecuteCore() +164
       System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
       System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
       System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
       System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
       System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
       System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
       System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
       System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
       System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
       System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
       System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
       System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8963149
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184



//I have below copy/paste some code from the project which I think may be relevant:


public class BaseController : Controller
{
    protected override void ExecuteCore()
    {
        string cultureName = null;
        // Attempt to read the culture cookie from Request
        HttpCookie cultureCookie = Request.Cookies["_culture"];
        if (cultureCookie != null)
            cultureName = cultureCookie.Value;
        else
            cultureName = Request.UserLanguages[0]; // obtain it from HTTP header AcceptLanguages

        // Validate culture name
        cultureName = CultureHelper.GetImplementedCulture(cultureName); // This is safe


        // Modify current thread's cultures            
        Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultureName);
        Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

        base.ExecuteCore();
    }
}


//And from the global.asax, this may be relevant


    void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e)
    {
        HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
        if (authCookie != null)
        {
            string encTicket = authCookie.Value;
            if (!String.IsNullOrEmpty(encTicket))
            {
                FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(encTicket);
                NerdIdentity id = new NerdIdentity(ticket);
                GenericPrincipal prin = new GenericPrincipal(id, null);
                HttpContext.Current.User = prin;
            }
        }
    }

    void MvcApplication_AuthenticateRequest(object sender, EventArgs e)
    {
    }

    /* NerdDinner i18n Custom caching */
    public override string GetVaryByCustomString(HttpContext context, string arg)
    {
        // It seems this executes multiple times and early, so we need to extract language again from cookie.
        if (arg == "culture") // culture name (e.g. "en-US") is what should vary caching
        {
            string cultureName = null;
            // Attempt to read the culture cookie from Request
            HttpCookie cultureCookie = Request.Cookies["_culture"];
            if (cultureCookie != null)
                cultureName = cultureCookie.Value;
            else
                cultureName = Request.UserLanguages[0]; // obtain it from HTTP header AcceptLanguages

            // Validate culture name
            cultureName = CultureHelper.GetImplementedCulture(cultureName); // This is safe

            return cultureName.ToLower();// use culture name as cache key, "es", "en-us", "es-cl", etc.
        }

        return base.GetVaryByCustomString(context, arg);
    }
}

希望有人可以看一看并告诉我什么拒绝谷歌抓取工具,这样我就无法取得任何东西。

1 个答案:

答案 0 :(得分:0)

Request.UserLangauges对象基于使用来自浏览器的请求。 googlebot的抓取工具不是浏览器,因此如果Request.UserLangauges不为空,则应先测试,如下所示:

    if (cultureCookie != null)
       cultureName = cultureCookie.Value;
    else {
       if (Request.UserLanguages != null && Request.UserLanguages.Length > 0)
           cultureName = Request.UserLanguages[0];
       else
           cultureName = "the culture you'd like to use";
    }