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
Get the same error:
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.
Request.UserLanguages
is null and is the reason why you are getting a NRE. The reason why this property is null is very simple: the robot didn't sent an Accept-Language
request header.
So fix your code by checking whether this property is not null before attempting to access it:
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();
}