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:
FilterAttribute
class, to ASP.Net MVC's
filter 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 inside ConfigureServices
method in Startup
class,but I do not know how to call this in ASP.Net MVC
!
public class Startup
{
//...Code cropped for the case of simplicity
public void ConfigureServices(IServiceCollection services)
{
services.MyCustomConfigure()
}
}
ASP.NET (WebForms, MVC, WebAPI) and ASP.NET Core are different frameworks and while they share similar concepts (like filter attributes), they are different and you cannot use components written for ASP.NET Core using its APIs (Microsoft.AspNetCore.*
) for classic ASP.NET.
This means that you will need to have different implementations for ASP.NET Core and ASP.NET MVC 5.