Asp.net 会话过期时将部分视图重定向到登录页面
会话过期后,是否有简单的方法将整个页面(而不仅仅是部分视图)重定向到登录页面 我尝试了以下解决方案,但无法使其工作:Asp.net 会话过期时将部分视图重定向到登录页面,asp.net,asp.net-mvc,session,asp.net-mvc-partialview,Asp.net,Asp.net Mvc,Session,Asp.net Mvc Partialview,会话过期后,是否有简单的方法将整个页面(而不仅仅是部分视图)重定向到登录页面 我尝试了以下解决方案,但无法使其工作: 我的问题是部分视图重定向到登录页面,而不是整个页面(与链接中的问题相同) 控制器 [HttpPost] public PartialViewResult LogPartialView(string a, int? b, string c, string d, int? e, string f) { /
[HttpPost]
public PartialViewResult LogPartialView(string a, int? b, string c, string d, int? e, string f)
{
//If the user is "Admin" -> display Logs for all customers.
if (Roles.IsUserInRole(WebSecurity.CurrentUserName, "Admin"))
{
if (Session["myID"] == null)
{
ExpireSession();
}
//Some code
return PartialView("LogPartialLayout", model);
}
如果myID为null,我想返回Redirect(~/),但它不起作用,因为它需要一个局部视图
错误消息:无法将类型“System.Web.Mvc.RedirectResult”隐式转换为“System.Web.Mvc.PartialViewResult”
public void ExpireSession()
{
Session.Abandon();
WebSecurity.Logout();
Response.Redirect("~/");
}
网络配置
<authentication mode="Forms">
<forms loginUrl="~/Account/RedirectToLogin" timeout="2880" />
</authentication>
_重定向到登录视图
<script>
window.location = '@Url.Action("Login", "Account")';
</script>
window.location='@Url.Action(“登录”、“帐户”);
诸如此类,相应地更改URL我将基于@EmilChirambattu的答案
[HttpPost]
public ActionResult LogPartialView(string a, int? b, string c, string d, int? e, string f)
{
// You should check the session before anything else.
if (Session["myID"] == null)
{
return ExpireSession();
}
//If the user is "Admin" -> display Logs for all customers.
if (Roles.IsUserInRole(WebSecurity.CurrentUserName, "Admin"))
{
//Some code
}
return PartialView("LogPartialLayout", model);
}
public void ExpireSession()
{
Session.Abandon();
WebSecurity.Logout();
Response.Redirect("RedirectToLogin");
}
public ActionResult RedirectToLogin()
{
return PartialView("_RedirectToLogin");
}
\u重定向到登录视图
<script>
window.location = '@Url.Action("Index", "")';
</script>
window.location='@Url.Action(“索引”,“索引”);
这会将您重定向到页面的基本URL(很可能是您的登录页面)。将方法的签名更改为:
public ActionResult LogPartialView(…)
@haim770 I added if session=null->redirect(~/”)但它仍然在局部视图中重定向。@haim770..我忘了提到我已更改为public ActionResult,但没有成功。仍然在PartialView中重定向。
<script>
window.location = '@Url.Action("Index", "")';
</script>