遇到文化和 NullReferenceException 问题
我正在尝试编写登陆页面代码,通过阅读文化将决定请求是否被重定向到英语网站或斯洛伐克语网站。
代码如下:
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strCountry = ResolveCountry().ToString();
if (strCountry == "SK")
{
Response.Redirect("/sk/");
}
else
{
Response.Redirect("/en/");
}
}
public static CultureInfo ResolveCulture()
{
string[] languages = HttpContext.Current.Request.UserLanguages;
if (languages == null || languages.Length == 0)
return null;
try
{
string language = languages[0].ToLowerInvariant().Trim();
return CultureInfo.CreateSpecificCulture(language);
}
catch (ArgumentException)
{
return null;
}
}
public static RegionInfo ResolveCountry()
{
CultureInfo culture = ResolveCulture();
if (culture != null)
return new RegionInfo(culture.LCID);
return null;
}
}
问题是在浏览器中它看起来没问题,它会将您重定向到该站点: http:// www.alexmolcan.sk
但是在检查 IIS 日志时,Google 网站管理员工具或 http://www.rexswain.com/httpview.html 我总是收到 500 ASP 错误:
Object·reference·not·set·to·an·instance·of·an·object.
System.NullReferenceException:·Object·reference·not·set·to·an·instance·of·an·object.
响应标头:
HTTP/1.1·500·Internal·Server·Error
Connection:·close
Content-Length:·4684
当我在本地调试项目时,它总是可以毫无问题地编译。我不知道我做错了什么
谢谢。
编辑
异常
Process information:
Process ID: 4068
Process name: w3wp.exe
Account name: IIS APPPOOL\ASP.NET v4.0
Exception information:
Exception type: NullReferenceException
Exception message: Object reference not set to an instance of an object.
at sk_alexmolcan._default.Page_Load(Object sender, EventArgs e) in default.aspx.cs:line 15
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
I am trying to code landing page which by reading Culture will decide whether request will be redirected to English site or Slovak site.
This is what the code looks like:
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strCountry = ResolveCountry().ToString();
if (strCountry == "SK")
{
Response.Redirect("/sk/");
}
else
{
Response.Redirect("/en/");
}
}
public static CultureInfo ResolveCulture()
{
string[] languages = HttpContext.Current.Request.UserLanguages;
if (languages == null || languages.Length == 0)
return null;
try
{
string language = languages[0].ToLowerInvariant().Trim();
return CultureInfo.CreateSpecificCulture(language);
}
catch (ArgumentException)
{
return null;
}
}
public static RegionInfo ResolveCountry()
{
CultureInfo culture = ResolveCulture();
if (culture != null)
return new RegionInfo(culture.LCID);
return null;
}
}
The problem is that in browser it looks ok, it redirects you to the site: http://www.alexmolcan.sk
But when checking IIS log, Google Webmaster tools or http://www.rexswain.com/httpview.html I always get 500 ASP Error:
Object·reference·not·set·to·an·instance·of·an·object.
System.NullReferenceException:·Object·reference·not·set·to·an·instance·of·an·object.
Response header:
HTTP/1.1·500·Internal·Server·Error
Connection:·close
Content-Length:·4684
When I debug project locally it always compile without any problems. I don't know what I do wrong
Thank you.
EDIT
Exception
Process information:
Process ID: 4068
Process name: w3wp.exe
Account name: IIS APPPOOL\ASP.NET v4.0
Exception information:
Exception type: NullReferenceException
Exception message: Object reference not set to an instance of an object.
at sk_alexmolcan._default.Page_Load(Object sender, EventArgs e) in default.aspx.cs:line 15
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你正在抛出,因为当 ResolveCountry 返回 null 时,你的 .ToString() 和 if(strcountry == "SK") 将抛出。
无法将 null 转换为字符串。尝试
I think you're throwing because when ResolveCountry returns null your .ToString() and if(strcountry == "SK") is going to throw.
Can't turn null into a string. try