【问题标题】:Why is ASP.NET MVC using the GET action parameter value for the View name?为什么 ASP.NET MVC 使用 GET 操作参数值作为视图名称?
【发布时间】:2017-03-09 20:14:44
【问题描述】:

您好,在 ASP.net MVC 网站上工作时,我遇到了一个奇怪的问题。 在重定向到控制器中的另一个动作时,我想添加一个 get 值,我确实喜欢这样做

    //
    // POST: /Account/Register
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.UserName, Email = model.Email };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                //await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
                return RedirectToAction("ConfirmEmailAwait", "Account", new { userId = user.Id });   //<-----[LOOK HERE]
            }
            AddErrors(result);
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

    //
    // GET: /Account/ConfirmEmailAwait?userId=d178b665-b616-4303-ae7d-00a663014109
    [AllowAnonymous]
    public async Task<ActionResult> ConfirmEmailAwait(string userId)
    {
        // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
        // Send an email with this link
        string code = await UserManager.GenerateEmailConfirmationTokenAsync(userId);
        var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = userId, code = code }, protocol: Request.Url.Scheme);

        string emailHeader = "Confirm your account";
        string emailBody = "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>";

        await UserManager.SendEmailAsync(userId, emailHeader, emailBody);

        return View(userId);
    }

据我所知,这段代码完成了它应该做的事情,它将用户发送到正确的 URL,因此它应该可以正常工作。但如果你在这里看这张图片:

Error image

构造的 URL:http://localhost:55767/Account/ConfirmEmailAwait?userId=d178b665-b616-4303-ae7d-00a663014109

错误消息:未找到视图“d178b665-b616-4303-ae7d-00a663014109”或其主视图,或者没有视图引擎支持搜索到的位置

你会看到它正在搜索的 View 是我给它的 get 值,我觉得很奇怪。

有人知道发生了什么吗?我只是犯了一个愚蠢的错误而没有看到吗?请帮帮我,提前谢谢你。

【问题讨论】:

  • 您不会重定向到视图。您重定向到操作。
  • 您需要显示两种控制器方法的代码(不是指向它的图像的链接),以便添加答案
  • @mason 啊谢谢,我现在修好了
  • @StephenMuecke 我更新了问题

标签: c# asp.net asp.net-mvc get


【解决方案1】:

您将string 作为return View() 的第一个参数传递,它使用this overload,其中参数是视图的名称。您需要使用 this overload 将其作为 object 传递,其中参数是您的模型。

return View((object)userId);

【讨论】:

    【解决方案2】:

    当您需要View(Object model) 时,您正在使用View(String viewName) 重载。

    要将String 值用作ViewModel,您需要先转换为Object...:

    return this.View( (Object)userId );
    

    ...或使用参数标签:

    return this.View( model: userId );
    

    使用参数标签是我的首选方法,因为转换为 Object 的原因对于代码的未来读者可能不会立即显而易见,但您可能还想添加注释,以便用户知道为什么显式命名参数以及为什么它不应该被删除,例如:

    return this.View( model: userId ); // Be careful not to call the `View(String viewName)` overload!
    

    【讨论】:

      【解决方案3】:
      // GET: /Account/ConfirmEmail
      [AllowAnonymous]
      public async Task<ActionResult> ConfirmEmailAwait(string userId)
      {
          // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
          // Send an email with this link
          string code = await UserManager.GenerateEmailConfirmationTokenAsync(userId);
          var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = userId, code = code }, protocol: Request.Url.Scheme);
      
          string emailHeader = "Confirm your account";
          string emailBody = "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>";
      
          await UserManager.SendEmailAsync(userId, emailHeader, emailBody);
      
          return View(userId);
      }
      

      在上面的代码中,注意“return View(userId);”

      “userId”是一个字符串 - 当返回 View(string) 时,Action 认为您的 userId 实际上是 View 的路径(.cshtml 文件)。

      这就是代码出现异常的原因,说它无法找到与提供的路径匹配的视图。

      我希望这是有道理的:-)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-21
        • 1970-01-01
        • 1970-01-01
        • 2011-05-24
        • 2017-01-09
        相关资源
        最近更新 更多