想要超链接到控制器方法而不是视图

时间:2013-01-14 10:01:56

标签: c# razor asp.net-mvc-4 hyperlink

我希望Logout链接执行位于HomeController的流程,而不是显示新的View。我该如何构建这个?

控制器方法:

public ActionResult LogoutProcess()
    {
        previousLoggedIn = WebSecurity.CurrentUserName;
        WebSecurity.Logout();
        return RedirectToAction("Logout", "Home");
    }

    public ActionResult Logout(HomeModels.LogoutModel model)
    {
        model.PreviouslyLoggedInUsername = previousLoggedIn;
        return View(model);
    }

查看:

<a href = "@Url.Action("LogoutProcess", "Home")">Logout</a>

3 个答案:

答案 0 :(得分:1)

您可以使用标准链接,定位该操作

@Url.Action("LogoutProcess", "Home")

“技巧”在重定向中,在LogoutProcess()操作结束时显示在其他视图中:

public ActionResult LogoutProcess()
{
    // TempData to transfer user name      
    TempData["previousLoggedIn"] = WebSecurity.CurrentUserName;
    WebSecurity.Logout();
    return RedirectToAction("Logout", "Home");
}

public ActionResult Logout(HomeModels.LogoutModel model)
{
    // fill model from TempData
    model.PreviouslyLoggedInUsername = TempData["previousLoggedIn"];
    return View(model);
}

CurrentUserName通过TempData

传递给其他操作

答案 1 :(得分:0)

试试这个:

public ActionResult LogoutProcess()
{
    WebSecurity.Logout();
    //return null;
    Return RedirectToAction("Index");//or whatever page you want to display after logout.
}

答案 2 :(得分:0)

您是否考虑过无内容的http状态代码结果?

return new HttpStatusCodeResult(HttpStatusCode.NoContent);