Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取错误值不能为null或空。参数名称:URL_C#_Asp.net Mvc_Single Sign On - Fatal编程技术网

C# 获取错误值不能为null或空。参数名称:URL

C# 获取错误值不能为null或空。参数名称:URL,c#,asp.net-mvc,single-sign-on,C#,Asp.net Mvc,Single Sign On,这是我的密码。请有人帮助我迫切需要上述代码 [HttpGet] public ActionResult Index(string returnUrl) { ViewBag.ReturnUrl = returnUrl; return View(); } [HttpPost] public ActionResult Index(LoginModel loginModel, string returnUrl) {

这是我的密码。请有人帮助我迫切需要上述代码

    [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);
    }
我正在关注这个链接:
问题出在哪里:

aa如果在没有返回URL参数的情况下点击操作,并将null传递给Redirect()方法,会发生什么情况您得到的正是这个错误:)

解决方案:

您可以检查url是否不为null,或者使用Microsoft在默认mvc模板中包含的RedirectToLocal方法(或者编写自己的或..等等,只是不要将null传递给重定向方法):


我没有看到名为
Url
的参数。你确定这都是你的代码吗?具体在哪一行?返回时获取错误。。返回重定向(returnUrl)@帕特里克霍夫曼:不,还有两个控制器。bt我返回时收到错误。
...
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");
    }
}