Reputation: 161
There is an example of how to make a CustomExceptionFilterAttribute at https://docs.asp.net/en/latest/mvc/controllers/filters.html I would like to do the same to make a CustomExceptionFilterAttribute with IHostingEnvironment as the parameter.
services.AddMvc(
config =>
{
config.Filters.Add(new CustomExceptionFilterAttribute(???));
});
I am trying to add filters in startup class, but I don't know how to supply the IHostingEnvironment parameter.
services.AddMvc has to be in method
public void ConfigureServices(IServiceCollection services)
I got error if I do
public void ConfigureServices(IServiceCollection services,IHostingEnvironment env)
Upvotes: 2
Views: 3009
Reputation: 24073
You can use Startup
constructor injection like this:
private readonly IHostingEnvironment _env;
public Startup(IHostingEnvironment env)
{
_env = env;
}
.....
services.AddMvc(
config =>
{
config.Filters.Add(new CustomExceptionFilterAttribute(_env));
});
See official docs https://docs.asp.net/en/latest/fundamentals/startup.html#services-available-in-startup
Upvotes: 1
Reputation: 66
I think what you would want to implement is IExceptionFilter Interface, and after implementation you can add some CustomExceptionFilter into ConfigureServices. CustomExceptionFilterAttribute need to be dealt with differently, because that is an Attribute.
Upvotes: 0