[英]Getting an error Value cannot be null or empty. Parameter name: URL
Here is my code. 这是我的代码。 Please some one help me desperately needed the code above mentioned.
请有人帮助我急需上面提到的代码。
[HttpGet]
public ActionResult Index(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
[HttpPost]
public ActionResult Index(LoginModel loginModel, string returnUrl)
{
if (ModelState.IsValid)
{
if (loginModel.Username == "user" && loginModel.Password == "password")
{
FormsAuthentication.SetAuthCookie(loginModel.Username, true);
return Redirect(returnUrl);
}
else
{
ModelState.AddModelError("", "The username or password provided is incorrect.");
}
}
ViewBag.ReturnUrl = returnUrl;
return View(loginModel);
}
And I am following this link: http://www.primaryobjects.com/CMS/Article155.aspx 我正在关注此链接: http : //www.primaryobjects.com/CMS/Article155.aspx
Where is the problem: 问题出在哪儿:
Aaand what happen if you hit your action without returnUrl parameter and you pass null to the Redirect() method ?? Aaand如果您在没有returnUrl参数的情况下执行操作并将null传递给Redirect()方法,会发生什么情况? - You get exactly this error :).
-您恰好收到此错误:)。
Solution: 解:
You can check if the url is not null or use the RedirectToLocal method which microsft has included in the default mvc template (or write your own or .. etc just don't pass null to the Redirect method): 您可以检查网址是否不为null或使用microsft包含在默认mvc模板中的RedirectToLocal方法(或编写您自己的或..等只是不要将null传递给Redirect方法):
...
FormsAuthentication.SetAuthCookie(loginModel.Username, true);
// Here 'return Redirect(returnUrl);' become:
return RedirectToLocal(returnUrl);
...
private ActionResult RedirectToLocal(string returnUrl)
{
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.