【问题标题】:return View("viewname") not returning the view specifiedreturn View("viewname") 不返回指定的视图
【发布时间】:2015-11-10 16:31:47
【问题描述】:

使用 SPA 模板,如果用户尚未验证他们的电子邮件,我正在尝试使用 Login POST 方法返回 VerifyEmail 视图。

在调试时,您可以看到 return View("VerifyEmail") 被调用,但是,我返回到“/”并且没有返回 VerifyEmail 视图。

我已验证视图位于正确的文件夹视图 > 帐户中。

我应该寻找一些自动路由配置吗?为什么我的视图没有返回?

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> LogOn(LogOnVM model, string returnUrl)
{
    if (!ModelState.IsValid)
        return View(model);

    switch (await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, true))
    {
        case SignInStatus.Success:
            MyUser user = await UserManager.FindByNameAsync(model.UserName);
            if (!await UserManager.IsEmailConfirmedAsync(user.UserName))
                return View("VerifyEmail");
            else
                return RedirectToLocal(returnUrl);
        //other cases ignored for brevity
    }
}

我试图添加这个,但根本没有帮助:

public ActionResult VerifyEmail()
{
    return View();
}

我在我的代码中搜索并发现了这个,显然是因为默认模板:

public override Task ValidateClientRedirectUri(OAuthValidateClientRedirectUriContext context)
{
    if (context == null)
        throw new ArgumentNullException("context");

    if (context.ClientId == _publicClientId)
    {
        Uri expectedRootUri = new Uri(context.Request.Uri, "/");

        if (expectedRootUri.AbsoluteUri == context.RedirectUri)
        {
            context.Validated();
        }
        else if (context.ClientId == "web")
        {
            var expectedUri = new Uri(context.Request.Uri, "/");
            context.Validated(expectedUri.AbsoluteUri);
        }
    }
    return Task.FromResult<object>(null);
}

这可能是导致问题的原因吗?

【问题讨论】:

  • 你试过用return RedirectToAction("VerifyEmail");代替吗?
  • @Jonnus,是的,我遇到了同样的“错误”
  • VerifyEmail 视图位于何处?
  • @enki.dev 视图在 Views 下,在与 Controller 同名的文件夹下
  • @cFrozenDeath,尝试在ValidateClientRedirectUri 中注释代码,尽管不应该这样,因为return View("..."); 不会重定向,它会呈现视图。

标签: c# asp.net-mvc


【解决方案1】:

在我上次编辑中注释代码不起作用,但我发现从 /account/logon?ReturnUrl=%2F 中删除 ?ReturnUrl=%2F 可以解决问题。 ReturnUrl 在@Html.BeginForm 下作为routeValues 传递,用于登录视图。我想我只需将null 传递给routeValues 即可解决问题!

【讨论】:

    猜你喜欢
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多