Url.IsLocal 抛出空引用异常
传入的 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" });
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题不在于 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.