在 .Net Framework 4.7 应用程序中使用 .Net Standard 库
Use .Net Standard library in .Net Framework 4.7 application
我有一组 class 库,它们是使用 .Net Standard 2.0 开发的。这些 class 库之一实现了位于 Microsoft.AspNetCore.Mvc.Filters
命名空间中的 IAuthorizationFilter
接口。
public class AuthorizationFilter : IAuthorizationFilter
{
public AuthorizationFilter() {}
public void OnAuthorization(AuthorizationFilterContext context)
{/*Code cropped for the case of simplicity*/}
}
另一个 class 库的主要职责是注册依赖注入配置。
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;
}
}
除了这些 .Net Standard class 库之外,我还有一个使用 ASP.Net MVC
(.Net Framework 4.7)
开发的 Web 应用程序项目
我这里有两个主要问题:
- 我需要将我的
FilterAttribute
class 添加到 ASP.Net MVC's
过滤器列表,这会引发异常:
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.
我在我的 ASP.Net MVC 应用程序中以这种方式注册了过滤器:
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
//...Code cropped for the case of simplicity
filters.Add(typeof(AuthorizationFilter));
}
}
显然我不想在我的 .Net Standard class 库中添加对 System.Web
的引用,另一方面,我不知道如何解决这个问题!
第二个问题,是我可以调用 services.MyCustomConfigure()
方法的地方。在 .Net Core 应用程序中,我在 Startup
class 中的 ConfigureServices
方法中调用了此方法,但我不知道如何在 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)和 ASP.NET Core 是不同的框架,虽然它们共享相似的概念(如过滤器属性),但它们是不同的,您不能使用为 ASP.NET 核心使用其 API (Microsoft.AspNetCore.*
) 用于经典 ASP.NET。
这意味着您将需要对 ASP.NET Core 和 ASP.NET MVC 5 进行不同的实现。
我有一组 class 库,它们是使用 .Net Standard 2.0 开发的。这些 class 库之一实现了位于 Microsoft.AspNetCore.Mvc.Filters
命名空间中的 IAuthorizationFilter
接口。
public class AuthorizationFilter : IAuthorizationFilter
{
public AuthorizationFilter() {}
public void OnAuthorization(AuthorizationFilterContext context)
{/*Code cropped for the case of simplicity*/}
}
另一个 class 库的主要职责是注册依赖注入配置。
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;
}
}
除了这些 .Net Standard class 库之外,我还有一个使用 ASP.Net MVC
(.Net Framework 4.7)
我这里有两个主要问题:
- 我需要将我的
FilterAttribute
class 添加到ASP.Net MVC's
过滤器列表,这会引发异常:
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.
我在我的 ASP.Net MVC 应用程序中以这种方式注册了过滤器:
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
//...Code cropped for the case of simplicity
filters.Add(typeof(AuthorizationFilter));
}
}
显然我不想在我的 .Net Standard class 库中添加对 System.Web
的引用,另一方面,我不知道如何解决这个问题!
第二个问题,是我可以调用
中调用此方法services.MyCustomConfigure()
方法的地方。在 .Net Core 应用程序中,我在Startup
class 中的ConfigureServices
方法中调用了此方法,但我不知道如何在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)和 ASP.NET Core 是不同的框架,虽然它们共享相似的概念(如过滤器属性),但它们是不同的,您不能使用为 ASP.NET 核心使用其 API (Microsoft.AspNetCore.*
) 用于经典 ASP.NET。
这意味着您将需要对 ASP.NET Core 和 ASP.NET MVC 5 进行不同的实现。