簡體   English   中英

HTTP_USER_AGENT請求IIS7.5的HTTP錯誤代碼500

[英]HTTP error code 500 for HTTP_USER_AGENT requests IIS7.5

我有在iis7.5上運行的MVC4應用程序。 它工作正常,但是google無法將其索引為服務器錯誤,響應代碼500,以及當我在以下服務之一中提交我的網址時:

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

http://validator.w3.org/

得到相同的錯誤:

在此處輸入圖片說明

在此處輸入圖片說明

在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;
        }
    }

}

那怎么可能出問題了? 非常感謝您的幫助。

Request.UserLanguages為null,這是獲得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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM