I have a set of class libraries which are developed using .Net Standard 2.0. One of these class libraries implements the IAuthorizationFilter interface which is resides in Microsoft.AspNetCore.Mvc.Filters namespace.
public class AuthorizationFilter : IAuthorizationFilter { public AuthorizationFilter() {} public void OnAuthorization(AuthorizationFilterContext context) {/*Code cropped for the case of simplicity*/} } The other class library's main responsibility is to register the Dependency Injection configuration.
public static class ServiceCollectionExtensions { public static IServiceCollection MyCustomConfigure(this IServiceCollection services) { //...Code cropped for the case of simplicity services.AddMvc(config => { config.Filters.Add(typeof(AuthorizationFilter)); }); return services; } } Beside these .Net Standard class libraries, I have a web application project developed using ASP.Net MVC(.Net Framework 4.7)
I have two main problem here:
- I need to add my
FilterAttributeclass, toASP.Net MVC'sfilter list, which throws an exception saying:
The given filter instance must implement one or more of the following filter interfaces: System.Web.Mvc.IAuthorizationFilter, System.Web.Mvc.IActionFilter, System.Web.Mvc.IResultFilter, System.Web.Mvc.IExceptionFilter, System.Web.Mvc.Filters.IAuthenticationFilter.
I registered the filter in this way in my ASP.Net MVC application:
public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { //...Code cropped for the case of simplicity filters.Add(typeof(AuthorizationFilter)); } } apparently I do not want to add reference to System.Web in my .Net Standard class libraries, on the other hand, I do not know how to resolve this problem!
The second problem, is the place where I can call my
services.MyCustomConfigure()method. In .Net Core applications I called this method insideConfigureServicesmethod inStartupclass,but I do not know how to call this inASP.Net MVC!public class Startup { //...Code cropped for the case of simplicity public void ConfigureServices(IServiceCollection services) { services.MyCustomConfigure() } }