Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.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# 需要通过构造函数或其他方式从Razor筛选器方法访问HttpContext_C#_Asp.net Core_Razor - Fatal编程技术网

C# 需要通过构造函数或其他方式从Razor筛选器方法访问HttpContext

C# 需要通过构造函数或其他方式从Razor筛选器方法访问HttpContext,c#,asp.net-core,razor,C#,Asp.net Core,Razor,我已经阅读了关于这件事的所有其他帖子,我确信这不是重复的 我正在构建一个Razor页面过滤器,用于我的Startup.cs类,我需要访问HttpContext。我甚至在Startup.cs中有通常的services.AddHttpContextAccessor()语句,并在我的项目中的其他地方使用它 由于我正在构建一个源于IAsyncPageFilter,并且是从Startup.cs类创建的,所以它看起来不像我可以注入它(因为它是由Startup创建的,而不是注入的) 下面是对Startup.c

我已经阅读了关于这件事的所有其他帖子,我确信这不是重复的

我正在构建一个Razor页面过滤器,用于我的
Startup.cs
类,我需要访问
HttpContext
。我甚至在
Startup.cs
中有通常的
services.AddHttpContextAccessor()
语句,并在我的项目中的其他地方使用它

由于我正在构建一个源于
IAsyncPageFilter
,并且是从
Startup.cs
类创建的,所以它看起来不像我可以注入它(因为它是由Startup创建的,而不是注入的)

下面是对
Startup.cs
添加的过滤器:

Startup.cs(snip)
services.AddMvc(选项=>
{
var policy=new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()文件
.Build();
options.Filters.Add(新的授权过滤器(策略));

options.Filters.Add(新RazorAsyncPageFilter(_logger,Configuration));将IHttpContextAccessor添加为构造函数参数

public RazorAsyncPageFilter(ILogger logger, IConfiguration configuration, IHttpContextAccessor httpContext)
{
    _logger = logger;
    _configuration = configuration;
    _httpContext = httpContext;
}
然后添加过滤器

options.Filters.Add()

我有一个想法,但它不是答案。因此,当我在Asp.Net MVC中做同样的事情时,我创建了一个新类,并从WebViewPage抽象类继承。F.e.:
抽象类TViewPage:System.Web.MVC.WebViewPage
您可以在Asp.Net Core中使用RazorPage类,而不是WebViewPage什么是
Vault GraphServiceClient.OnMsalUiExCeptioneEvent
@Nkosi它是对从另一个静态类触发的事件的订阅。有趣。那么,按类型添加筛选器如何更改DI行为?看起来我可以只指定ILogger、IConfiguration和IHttpContextAccessor而不传递它们。它们似乎在RazorSyncPageFilter类中可用,而不传递them from Startup.cs。当我之前将它们添加到构造函数中时,Startup.cs类希望将它们全部传入。@JasonShave MSFT可能我不理解你的问题,但它不会以任何方式改变DI行为,它只是使用它。在初始代码中,你自己实例化了过滤器,而框架DI根本没有涉及
    public class RazorAsyncPageFilter : IAsyncPageFilter
    {
        private readonly ILogger _logger;
        private readonly IConfiguration _configuration;

        public string[] Scopes { get; set; }
        public string ScopeKeySection { get; set; }

        public RazorAsyncPageFilter(ILogger logger, IConfiguration configuration)
        {
            _logger = logger;
            _configuration = configuration;
        }
        public async Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context)
        {
            await Task.CompletedTask;
        }

        public async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next)
        {
            VaultGraphServiceClient.OnMsalUiExceptionEvent += VaultGraphServiceClientOnOnMsalUiExceptionEvent;
            await next.Invoke();
        }
        
        private void VaultGraphServiceClientOnOnMsalUiExceptionEvent(object sender, MsalUiRequiredException e)
        {
            _logger.LogInformation($"Triggered authentication exception: {e.Message}");
            Scopes = new string[] { _configuration.GetValue<string>(ScopeKeySection) };
            var properties = BuildAuthenticationPropertiesForIncrementalConsent(Scopes, e, **I_NEED_CONTEXT_HERE**);
            new ChallengeResult(properties);
        }
    }
public RazorAsyncPageFilter(ILogger logger, IConfiguration configuration, IHttpContextAccessor httpContext)
{
    _logger = logger;
    _configuration = configuration;
    _httpContext = httpContext;
}
options.Filters.Add<RazorAsyncPageFilter>()