0

嗨,我一直在使用国际版:ASP.NET MVC 3 Internationalization - Part 2 (NerdDinner): http ://afana.me/post/aspnet-mvc-internationalization-part-2.aspx

不幸的是,这包含一个主要错误。问题是我已经下载了代码/项目并进行了构建并将其发布到本地文件夹(有一些小错误需要修复才能发布),然后将文件上传到我的 webhotel。主页看起来还可以,但后来我查看了谷歌网站管理员工具并在网站上进行了爬网,发现网站中没有一个页面可以被索引,甚至根节点也无法被谷歌爬虫获取:

这是我在尝试获取根 (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);
    }
}

希望有人可以看看并告诉什么是拒绝谷歌爬虫,这样我就无法获取任何东西。

4

1 回答 1

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";
    }
于 2013-02-16T09:54:07.313 回答