在操作方法之后调用ViewComponent

时间:2016-01-13 05:04:45

标签: c# asp.net-core asp.net-core-identity asp.net-core-viewcomponent

我有以下布局页面:

<body>
<nav>@Component.Invoke("Navigation")</nav>
<main>@RenderBody()</main>
</body>

ViewComponent,为经过身份验证且未经过身份验证的用户呈现两个不同的视图:

public class NavigationViewComponent : ViewComponent
{
    public IViewComponentResult Invoke()
    {
        if(User.Identity.IsAuthenticated)
        {
            return View("User");
        }
        return View("Guest");
    }
}

我还有一个操作方法来注销用户:

public class HomeController : Controller
    ...
    public async Task<IActionResult> LogOut()
    {
        await _signInManager.SignOutAsync();
        return View("Index");
    }
}

我想在调用LogOut操作方法之后渲染ViewComponent ,该怎么做?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我需要做的就是RedirectToAction(),而不是返回视图。