Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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
Asp.net core ExceptionFilterAttribute.net核心_Asp.net Core - Fatal编程技术网

Asp.net core ExceptionFilterAttribute.net核心

Asp.net core ExceptionFilterAttribute.net核心,asp.net-core,Asp.net Core,下面是一个示例,说明如何在 我也希望使用IHostingEnvironment作为参数创建CustomExceptionFilterAttribute services.AddMvc( config => { config.Filters.Add(new CustomExceptionFilterAttribute(???)); }); 我试图在startup类中添加过滤器,但我不知道如何提供IHostingEnvi

下面是一个示例,说明如何在 我也希望使用IHostingEnvironment作为参数创建CustomExceptionFilterAttribute

services.AddMvc(
        config =>
        {
            config.Filters.Add(new CustomExceptionFilterAttribute(???));
        });
我试图在startup类中添加过滤器,但我不知道如何提供IHostingEnvironment参数

services.AddMvc(
        config =>
        {
            config.Filters.Add(new CustomExceptionFilterAttribute(???));
        });
services.AddMvc必须在方法中

public void ConfigureServices(IServiceCollection services)
如果我这样做了,我就错了

public void ConfigureServices(IServiceCollection services,IHostingEnvironment env)

我认为您想要实现的是,在实现之后,您可以将一些CustomExceptionFilter添加到ConfigureServices中。CustomExceptionFilterAttribute需要以不同的方式处理,因为这是一个属性

您可以像这样使用
启动
构造函数注入:

private readonly IHostingEnvironment _env;
public Startup(IHostingEnvironment env)
{
    _env = env;
}


参见官方文档

是的……我还没有意识到我可以像其他任何类一样注入startup类。