gpt4 book ai didi

c# - 发送 http header 后服务器无法设置状态

转载 作者:太空狗 更新时间:2023-10-30 00:31:58 24 4
gpt4 key购买 nike

我有 OnActionExecuting ActionFilter,我在其中检查了用户 session 是否已过期,如果 session 已过期,则用户必须重定向到登录页面

CompressFilterAttribute.cs 文件的代码如下:

public override void OnActionExecuting(ActionExecutingContext FilterContext)
{
if (((((System.Web.Mvc.ControllerContext)(FilterContext)).HttpContext).Request).IsAuthenticated)
GZipEncodePage(); //Function to check Session Expiration Time

var action = (string)FilterContext.RouteData.Values["action"];

if (!FilterContext.HttpContext.User.Identity.IsAuthenticated &&
action != "Index" &&
action != "FindStoreNameByText" &&
action.ToLower() != "sessiontimeout" &&
action.ToLower() != "logout")
{
string UrlSucesso = "/Home";
string UrlRedirecionar = string.Format("?ReturnUrl={0}", UrlSucesso);
string UrlLogin = FormsAuthentication.LoginUrl + UrlRedirecionar;
FilterContext.HttpContext.Response.Redirect(UrlSucesso, true);
}
}

CustomErrorHandleAttribute.cs 文件的代码如下所示:

public override void OnException(ExceptionContext filterContext)
{
base.OnException(filterContext);
// Rest logic
}

CustomErrorHandleAttribute.cs 文件中出现错误 --> 在发送 http header 后服务器无法设置状态

请帮我解决这个问题。

最佳答案

您的 OnActionExecution 方法发送必要的重定向 http header ,但它不会停止执行 Controller 代码。

我认为您应该使用以下内容来重定向用户而不是 Response.Redirect:

 FilterContext.Result = new RedirectResult(url); 

另请参阅这些其他堆栈溢出问题:

关于c# - 发送 http header 后服务器无法设置状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21014614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com