简体   繁体   English

在.Net Framework 4.7应用程序中使用.Net标准库

[英]Use .Net Standard library in .Net Framework 4.7 application

I have a set of class libraries which are developed using .Net Standard 2.0. 我有一组使用.Net Standard 2.0开发的类库。 One of these class libraries implements the IAuthorizationFilter interface which is resides in Microsoft.AspNetCore.Mvc.Filters namespace. 这些类库之一实现了IAuthorizationFilter接口,该接口位于Microsoft.AspNetCore.Mvc.Filters命名空间中。

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) 除了这些.Net Standard类库之外,我还有一个使用ASP.Net MVC (.Net Framework 4.7)开发的Web应用程序项目。

I have two main problem here: 我这里有两个主要问题:

  1. I need to add my FilterAttribute class, to ASP.Net MVC's filter list, which throws an exception saying: 我需要将我的FilterAttribute类添加到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. 给定的筛选器实例必须实现以下一个或多个筛选器接口: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: 我以这种方式在ASP.Net MVC应用程序中注册了过滤器:

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! 显然,我不想在.Net Standard类库中添加对System.Web引用,另一方面,我也不知道如何解决此问题!

  1. The second problem, is the place where I can call my services.MyCustomConfigure() method. 第二个问题是可以在哪里调用我的services.MyCustomConfigure()方法。 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 ! 在.Net Core应用程序中,我在Startup类的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) 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. ASP.NET(WebForms,MVC,WebAPI)和ASP.NET Core是不同的框架,尽管它们共享相似的概念(如过滤器属性),但它们是不同的,并且您不能使用通过其API为ASP.NET Core编写的组件Microsoft.AspNetCore.* ),用于经典ASP.NET。

This means that you will need to have different implementations for ASP.NET Core and ASP.NET MVC 5. 这意味着您将需要针对ASP.NET Core和ASP.NET MVC 5具有不同的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM