Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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


1 Answers

Where is the problem:

Aaand what happen if you hit your action without returnUrl parameter and you pass null to the Redirect() method ?? - 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):

...
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);
    }

    return RedirectToAction("Index", "Home");
}
like image 195
Viktor Bahtev Avatar answered Dec 14 '25 01:12

Viktor Bahtev