简体   繁体   English

收到错误值不能为null或为空。 参数名称:URL

[英]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.

相关问题 值不能为 null 或为空。 参数名称linkText - Value cannot be null or empty. Parameter name linkText 如何修复 MVC 应用程序中的“System.ArgumentException:'值不能为 null 或为空。参数名称:名称'”? - How to fix a "System.ArgumentException: 'Value cannot be null or empty. Parameter name: name'" in a MVC app? MVC5 Razor 值不能为 null 或为空。 参数名称:名称 - MVC5 Razor Value cannot be null or empty. Parameter name: name 值不能为null或为空。\ r \ nParameter name:name - Value cannot be null or empty.\r\nParameter name: name HTML ActionLink运行时错误:“值不能为null或为空。 但值不为null或为空 - Html ActionLink run time error: 'Value cannot be null or empty. but value is not null or empty ASP.NET Identity CreateAsync返回错误“名称不能为null或为空。” - ASP.NET Identity CreateAsync returning Error “Name cannot be null or empty.” 无法通过连接字符串连接到Dynamics CRM Online。 组织不能为null或为空。 参数名称:组织名称 - Unable to connect via connection string to Dynamics CRM Online. Organization cannot be null or empty. Parameter name: Organization Name 获取值不能为空。 参数名称:项目,MVC4 selectList错误? - Getting Value cannot be null. Parameter name: items, MVC4 selectList error? 值不能为空。 参数名称:字符串我遇到此错误 - Value cannot be null. Parameter name: String i am getting this error 使用Entity Framework在POST上获取错误-值不能为null。 参数名称:来源 - Getting error on POST with Entity Framework - Value cannot be null. Parameter name: source
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM