【问题标题】:Cannot redirect to specific view in ASP.NET MVC无法重定向到 ASP.NET MVC 中的特定视图
【发布时间】:2015-05-07 21:22:40
【问题描述】:

我正在通过尝试将 .NET Web 应用程序转换为 ASP.NET MVC 来学习 ASP.NET MVC。 我有一个控制器和登录视图。 如果用户通过身份验证,我想重定向到默认视图。

这是文件夹结构:

Views
  - Account
  - Home
  - Shared
Default.cshtml

这是控制器中的登录操作:

public ActionResult Login(LoginModel model)
{
    if (ModelState.IsValid)
    {
         bool authenticated = Security.AuthenticateLANUser(model.UserName, model.Password);
         if (!authenticated)
            {
                Session["authenticated"] = false;
                System.Text.StringBuilder errorMsg = new System.Text.StringBuilder();
                errorMsg.Append("Invalid Login/Password entered.");
                errorMsg.Append("We were not able to authenticate you in in Active Directory based on the information entered, ");
                errorMsg.Append("but we recorded your attempt for audit purposes.");

                ModelState.AddModelError("", errorMsg.ToString());
                return View(model);
            }
            else
            {
                return View("Views/Account/Default.cshtml");
            }
        }

        ModelState.AddModelError("", "The user name or password provided is incorrect.");
        return View(model);
    }

用户已通过身份验证,我应该被重定向到默认视图,但它不会重定向。我做错了什么?

【问题讨论】:

  • 您的代码中没有重定向。它必须是 return RedirectToAction("Default", "Account"); 假设你有一个 AccountController 和 Default 方法

标签: asp.net asp.net-mvc asp.net-mvc-4


【解决方案1】:

这应该是视图名称,而不是视图路径。所以:

return View("Default");

此外,默认视图应在与当前控制器(可能是帐户)对应的文件夹中或在共享文件夹中定义。您使用的路径表明它在 Account 中,您概述的结构表明它只是在 Views 文件夹中,所以我认为它也应该被提及。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-07
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多