c# - 返回 View 后未调用 ASP.NET 索引方法

标签 c# asp.net asp.net-mvc visual-studio

所以我是 ASP.Net 的新手,遇到了一些问题。问题是当我想调用一个新的 Controller 并显示相应的 View 时,该 Controller 中的索引方法没有被激活。

我的 LoginController 中有这段代码(我知道这是不安全的):

public ActionResult LoginTest(string inputEmail, string inputPassword, string submit)
{
    if(submit != null)
    {
        Session["email"] = inputEmail;
        return View("~/Views/Home/index.cshtml");                
    }
    return null;
}

这是我调用的 Controller / View 中的代码 (HomeController):

public ActionResult Index()
{
    if(Session["email"] != null)
    {
        ViewBag.HelloWorld = Session["email"].ToString();
    } else
    {
        ViewBag.HelloWorld = "Does not work.";
    }
    return View();
}

难道我不应该通过 return View() 来做到这一点吗?有没有调用 Controller 的方法调用相应的 View ?

感谢任何帮助。

最佳答案

使用RedirectToAction在你的 LoginController 而不是 return View("~/Views/Home/index.cshtml");:

return RedirectToAction("Index", "Index");

关于c# - 返回 View 后未调用 ASP.NET 索引方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36793095/

相关文章:

ASP.NET webforms 4.5 MapPageRoute Route Constraint w/regex 不起作用

asp.net-mvc - 日期格式不正确的 MVC DateTime 绑定(bind)

asp.net-mvc - 如何从 umbraco mvc 中的表面 Controller 添加查询字符串以保留模型值

c# - 使用 office 365/SharePoint 在线验证客户端

C# 2010 Microsoft Chart 4.0 控件没有 ChartType 属性?

c# - 将十六进制枚举参数从 C# 传递到 C

javascript - JavaScript 脚本标记中的绝对路径

c# - 给字符串一个空值

C# 版本的 WebForms 页面不工作,VB 版本是

asp.net-mvc - 了解Global.asax中的路由(asp.net-mvc)