Url.IsLocal 抛出空引用异常

发布于 2025-01-12 06:18:21 字数 515 浏览 4 评论 0原文

传入的 URL 为:"/Reporting/Dashboard"

为什么 Url.get 返回 null

public ActionResult RedirectToLocal(string returnUrl)
{
    if (Url.IsLocalUrl(returnUrl))
    {
        return Redirect(returnUrl);
    }
    else
    {
        return RedirectToAction("Index", "Dashboard", new { area = "Reporting" });
    }
}

输入图片此处描述

Url passed in is: "/Reporting/Dashboard"

Why is the Url.get returning null?

public ActionResult RedirectToLocal(string returnUrl)
{
    if (Url.IsLocalUrl(returnUrl))
    {
        return Redirect(returnUrl);
    }
    else
    {
        return RedirectToAction("Index", "Dashboard", new { area = "Reporting" });
    }
}

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

泪意 2025-01-19 06:18:21

这里的问题不在于 returnUrl,而在于 Controller.Url 属性,该属性为 null,因此您无法访问 null 对象的 IsLocalUrl() 方法。

如果没有更多信息,很难理解问题出在哪里,但这可能与您的参考资料有关。

这是相关问题: 为什么是 Controller.Url null 当我对我的操作进行单元测试时?

我建议使用此实现创建新的简单项目,看看问题是否仍然存在。

Your problem here is not with returnUrl, but with Controller.Url property, which is null, so you can't access IsLocalUrl() method of null object.

Without more information it is hard to undestand where is the problem, but this could something with your refferences.

Here is related issue: Why is Controller.Url null when I unit test my action?

I would suggest to create fresh new simple project with this implementation and see if problem still persits.

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