1

I have MVC4 application which runs on iis7.5. It works fine, but google can't index it saying server error, response code 500 and also when i submit my url on one of these services:

https://developers.google.com/speed/pagespeed/insights

http://validator.w3.org/

Get the same error:

enter image description here

enter image description here

In the elmah logs appears:

System.NullReferenceException: Object reference not set to an instance of an object.
   at erad.Controllers.BaseController.ExecuteCore()
   at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0()
   at System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)
   at System.Web.Mvc.MvcHandler.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
   at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

And here is BaseController (all application controllers inherit from BaseController)

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();
    }

    protected override bool DisableAsyncSupport
    {
        get
        {
            return true;
        }
    }

}

So what could be wrong? Any help is highly appreciated.

4

1 回答 1

1

Request.UserLanguages为空,这就是您获得 NRE 的原因。这个属性为空的原因很简单:机器人没有发送Accept-Language请求头。

因此,通过在尝试访问之前检查此属性是否不为空来修复您的代码:

protected override void ExecuteCore()
{
    // set some default value which will be used if all other attempts fail
    string cultureName = "en-US";

    // Attempt to read the culture cookie from Request
    HttpCookie cultureCookie = Request.Cookies["_culture"];
    if (cultureCookie != null)
    {
        cultureName = cultureCookie.Value;
    }
    else if (Request.UserLanguages != null)
    {
        // The user agent sent a Accept-Language request header so attempt to read its value
        cultureName = Request.UserLanguages[0]; 
    }

    // 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();
}
于 2013-06-05T10:50:38.350 回答