简体   繁体   English

NullRefrenceException未被用户代码处理

[英]NullRefrenceException was unhandled by user code

Code below is placed in page_Load. 下面的代码放在page_Load中。 How I should handle this to bypass UrlReferrer when you enter page directly first time and there is no referrer? 当你第一次直接输入页面并且没有推荐人时,我应该如何处理这个以绕过UrlReferrer? What I am missing here? 我在这里缺少什么?

    if (HttpContext.Current.Request.UrlReferrer.AbsoluteUri != null)
    {
        urlReferer = HttpContext.Current.Request.UrlReferrer.AbsoluteUri.ToString();
    }
    else
    {
        urlReferer = "";
    }

Just check UrlReferrer for null: 只需检查UrlReferrer是否为null:

if (HttpContext.Current.Request.UrlReferrer != null 
    && HttpContext.Current.Request.UrlReferrer.AbsoluteUri != null)
{
     urlReferer = HttpContext.Current.Request.UrlReferrer.AbsoluteUri.ToString();
}     
else     
{         
    urlReferer = "";     
} 

Who says the client passed by the referrer in the HTTP request? 谁说客户端在HTTP请求中通过引用者传递?

Check if UrlReferrer is null first 首先检查UrlReferrer是否为null

if (HttpContext.Current.Request.UrlReferrer != null)
    {
        urlReferer = HttpContext.Current.Request.UrlReferrer.AbsoluteUri.ToString();
    }
    else
    {
        urlReferer = "";
    }

我相信你需要检查HttpContext.Current.Request.UrlReferrer != null

Why not this way much cleaner than checking nulls 为什么不这样做比检查空值更清晰

private void Page_Load()
{
    if (!IsPostBack)
    {
        if (HttpContext.Current.Request.UrlReferrer != null)
        {
            urlReferer = HttpContext.Current.Request.UrlReferrer.AbsoluteUri.ToString();
        }
        else
        {
            urlReferer = "";
        }
    }
}

If UrlReferrer is null, then the test to AbsolutUri will fail. 如果UrlReferrer为null,则对AbsolutUri的测试将失败。

Try initially testing UrlReferrer for null, this will probably correct the issue. 尝试最初测试UrlReferrer为null,这可能会纠正这个问题。

Use your debugger. 使用调试器。 If you're running this out of visual studio, than you might be brought to a debugger window when the exception is thrown. 如果你在visual studio中运行它,那么在抛出异常时你可能会被带到调试器窗口。 There are multiple tabs at the bottom of the debugger including "Locals" and "Watch" you can use those to see what variables are being stored. 调试器底部有多个选项卡,包括“Locals”和“Watch”,您可以使用这些选项卡查看存储的变量。

If the above code is indeed what's causing the problem than 如果上面的代码确实是导致问题的原因

HttpContext.Current.Request.UrlReferrer.AbsoluteUri
or 要么
HttpContext.Current.Request.UrlReferrer
or 要么
HttpContext.Current.Request
or 要么
HttpContext.Current
or 要么
HttpContext

is set to null 设置为null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM