Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 操作筛选器正在启动,但重定向不是';不可能_C#_Asp.net Mvc_Action Filter - Fatal编程技术网

C# 操作筛选器正在启动,但重定向不是';不可能

C# 操作筛选器正在启动,但重定向不是';不可能,c#,asp.net-mvc,action-filter,C#,Asp.net Mvc,Action Filter,我发现在会话过期时捕获以下代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace SuburbanCustPortal.MiscClasses { public class SessionExpireFilterAttribute : ActionFilterAttribute { p

我发现在会话过期时捕获以下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SuburbanCustPortal.MiscClasses
{
    public class SessionExpireFilterAttribute : ActionFilterAttribute {

        public override void OnActionExecuting( ActionExecutingContext filterContext ) {
            HttpContext ctx = HttpContext.Current;

            // check if session is supported
            if ( ctx.Session != null ) {

                // check if a new session id was generated
                if ( ctx.Session.IsNewSession ) {

                    // If it says it is a new session, but an existing cookie exists, then it must
                    // have timed out
                    string sessionCookie = ctx.Request.Headers[ "Cookie" ];
                    if ( ( null != sessionCookie ) && ( sessionCookie.IndexOf ( "ASP.NET_SessionId" ) >= 0 ) ) 
                    {
                      ctx.Response.Redirect("~/Home/Login");
                    }
                }
            }

            base.OnActionExecuting ( filterContext );
        }
    }
}
它发射时没有问题,但被调用的操作仍在按其路线运行:

[Authorize]
[SessionExpireFilter]
public ActionResult PrePayment(PaymentModel.PrePayment model)
{
  if (string.IsNullOrWhiteSpace(Session[SessionEnums.CurrentAccountGuid.ToString()].ToString()))
  {
    return RedirectToAction("AddCustomer", "Customer");
  }

  if (TempData["ViewData"] != null)
  {
    ViewData = (ViewDataDictionary) TempData["ViewData"];
  }

[SessionExpireFilter]
正在该代码上运行,但随后它将继续使用方法的其余部分,这会导致问题,因为会话不再有效。我在
预付款
的第一行收到一个错误,因为它正在返回

重定向不应该阻止这种情况发生吗

编辑**

我做了建议的更改,现在:

命名空间SuburbanCustomPortal.Misclasses { 公共类SessionExpireFilterAttribute:ActionFilterAttribute{

    public override void OnActionExecuting( ActionExecutingContext filterContext ) 
    {
        HttpContext ctx = HttpContext.Current;

        // check if session is supported
        if ( ctx.Session != null ) {

            // check if a new session id was generated
            if ( ctx.Session.IsNewSession ) {

                // If it says it is a new session, but an existing cookie exists, then it must
                // have timed out
                string sessionCookie = ctx.Request.Headers[ "Cookie" ];
                if ( ( null != sessionCookie ) && ( sessionCookie.IndexOf ( "ASP.NET_SessionId" ) >= 0 ) ) 
                {
                  //ctx.Response.Redirect("~/Account/LogOn/");
                  filterContext.Result = new RedirectResult("~/Account/LogOn");
                  return;
                }
            }
        }

        base.OnActionExecuting ( filterContext );
    }
}
}

AccountScreen正在调用预付款,并且在返回后似乎正在重新加载预付款。

使用此选项:

   filterContext.Result = new RedirectResult("~/Home/Login");
   return;
而不是:

ctx.Response.Redirect("~/Home/Login");
使用以下命令:

   filterContext.Result = new RedirectResult("~/Home/Login");
   return;
而不是:

ctx.Response.Redirect("~/Home/Login");

在每个代码路径中,都会调用base.OnActionExecuting。您是否尝试在设置响应后立即返回?而不是运行
base.OnActionExecuting
?是。编辑:请参阅@YairNevet answer。在每个代码路径中,都会调用base.OnActionExecuting。您是否尝试在设置响应后立即返回?而不是运行t他
base.OnActionExecuting
?是。编辑:请参阅@YairNevet answer。有没有办法将此重定向到控制器而不是网页?@ErocM在MVC中没有网页。当您指定“~/Home/Login”时,它相当于:Home controller=>Login Action(方法)。我原以为它在工作,但帐户屏幕正在被召回。我已发布了更改后的代码。AccountScreen是调用预付款的原始表单。它正在重新加载…@ErocM我猜您正在使用表单身份验证模式,因此将用户重定向到何处并不重要,因为在任何情况下,如果用户未经身份验证icated,他将被重定向到您的默认登录/帐户操作。这不是由以下行决定的吗:routes.maprote(“default”,“{controller}/{action}/{id}”,new{controller=“account”,action=“Logon”,id=UrlParameter.Optional});有没有办法将其重定向到控制器而不是网页?@ErocM在MVC中没有网页。当您指定“~/Home/Login”时,它相当于:Home controller=>Login Action(method)。我原以为它在工作,但帐户屏幕正在被召回。我已发布了更改后的代码。AccountScreen是调用预付款的原始表单。它正在重新加载…@ErocM我猜您正在使用表单身份验证模式,因此将用户重定向到何处并不重要,因为在任何情况下,如果用户未经身份验证icated,他将被重定向到您的默认登录/帐户操作。这不是由以下行决定的吗:routes.maprote(“default”,“{controller}/{action}/{id}”,new{controller=“account”,action=“Logon”,id=UrlParameter.Optional});