HTTP_USER_AGENT的HTTP错误代码500请求IIS7.5

时间:2013-06-05 10:23:09

标签: .net asp.net-mvc-4 iis-7.5

我有运行在iis7.5上的MVC4应用程序。它工作正常,但谷歌无法索引它说服务器错误,响应代码500,当我在其中一个服务上提交我的网址时:

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

http://validator.w3.org/

得到同样的错误:

enter image description here

enter image description here

在elmah日志中出现:

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)

这是BaseController(所有应用程序控制器都从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;
        }
    }

}

那可能是什么错?任何帮助都非常感谢。

1 个答案:

答案 0 :(得分:1)

Request.UserLanguages为空,这就是您获得NRE的原因。此属性为null的原因非常简单:机器人未发送Accept-Language请求标头。

因此,在尝试访问此属性之前,请检查此属性是否为null,以修复代码:

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