2013-06-05 50 views
1

我有運行在iis7.5上的MVC4應用程序。它工作正常,但谷歌不能索引它說服務器錯誤,響應代碼500,並且還當我提交有關這些服務的一個我的網址:HTTP_USER_AGENT請求的HTTP錯誤代碼500 IIS7.5

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

Request.UserLanguages爲空,並且是您獲得NRE的原因。該屬性爲null的原因非常簡單:機器人未發送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(); 
} 
+0

謝謝你,它的工作原理。 –

相關問題