遇到文化和 NullReferenceException 问题

发布于 2024-12-01 06:43:40 字数 2401 浏览 1 评论 0原文

我正在尝试编写登陆页面代码,通过阅读文化将决定请求是否被重定向到英语网站或斯洛伐克语网站。

代码如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巴黎夜雨 2024-12-08 06:43:40

我认为你正在抛出,因为当 ResolveCountry 返回 null 时,你的 .ToString() 和 if(strcountry == "SK") 将抛出。

无法将 null 转换为字符串。尝试

CultureInfo cul = ResolveCountry();
string strCountry = cul== null ? string.empty : cul.ToString();

if (strCountry == "SK") {}

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

CultureInfo cul = ResolveCountry();
string strCountry = cul== null ? string.empty : cul.ToString();

if (strCountry == "SK") {}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文