首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NerdDinner的国际版本

NerdDinner的国际版本
EN

Stack Overflow用户
提问于 2013-02-16 16:25:51
回答 1查看 235关注 0票数 0

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

不幸的是,这包含了一个主要的错误。问题是,我已经下载了代码/项目,创建了一个构建,并将其发布到本地文件夹(有一些小错误需要修复才能进行发布),然后将文件上传到我的网络酒店。主页看起来工作正常,但后来我查看了Google Web Master工具并在站点上爬行,发现站点中没有一个页面可以被索引,甚至根节点都不能被Google爬虫抓取:

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

页面无法访问

HTTP/1.1 500内部服务器错误

代码语言:javascript
复制
    [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);
    }
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-16 17:54:07

Request.UserLangauges对象基于使用来自浏览器的请求。谷歌机器人的爬虫不是浏览器,因此你应该首先测试Request.UserLangauges是否为空,如下所示:

代码语言:javascript
复制
    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";
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14908429

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档