[ABP框架]动态web Api的拦截用法。

时间:2022-01-26 17:23:14

先进行配置

首先这种需求,一般发生在APP端,我们给APP,不会给所有项目系统的接口给他们用。我们系统有200个接口,但是APP的需求只会用20个。那么这个需求也就应运而生了。

[ABP框架]动态web Api的拦截用法。

以上为API文件夹中为项目接口,没有啥特别复杂的东西存在。

首先创建好,一个网站项目,进行模块的配置。

首先引入以下的nuget包:

以下是WebApp项目中的packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Abp" version="1.5.1" targetFramework="net452" />
<package id="Abp.Castle.Log4Net" version="1.5.1" targetFramework="net452" />
<package id="Abp.EntityFramework" version="1.5.1" targetFramework="net452" />
<package id="Abp.EntityFramework.Common" version="1.5.1" targetFramework="net452" />
<package id="Abp.Owin" version="1.5.1" targetFramework="net452" />
<package id="Abp.Web" version="1.5.1" targetFramework="net452" />
<package id="Abp.Web.Api" version="1.5.1" targetFramework="net452" />
<package id="Abp.Web.Common" version="1.5.1" targetFramework="net452" />
<package id="Abp.Web.Mvc" version="1.5.1" targetFramework="net452" />
<package id="Abp.Web.Resources" version="1.5.2" targetFramework="net452" />
<package id="Abp.Zero" version="1.5.1" targetFramework="net452" />
<package id="Abp.Zero.EntityFramework" version="1.5.1" targetFramework="net452" />
<package id="Abp.Zero.Owin" version="1.5.1" targetFramework="net452" />
<package id="Antlr" version="3.5.0.2" targetFramework="net452" />
<package id="bootstrap" version="3.0.0" targetFramework="net452" />
<package id="Castle.Core" version="3.3.3" targetFramework="net452" />
<package id="Castle.LoggingFacility" version="3.4.0" targetFramework="net452" />
<package id="Castle.Windsor" version="3.4.0" targetFramework="net452" />
<package id="EntityFramework" version="6.1.3" targetFramework="net452" />
<package id="EntityFramework.DynamicFilters" version="1.4.11" targetFramework="net452" />
<package id="JetBrains.Annotations" version="10.4.0" targetFramework="net452" />
<package id="jQuery" version="1.6.4" targetFramework="net452" />
<package id="log4net" version="2.0.8" targetFramework="net452" />
<package id="Microsoft.AspNet.Identity.Core" version="2.2.1" targetFramework="net452" />
<package id="Microsoft.AspNet.Identity.Owin" version="2.2.1" targetFramework="net452" />
<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.Mvc.zh-Hans" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.Razor.zh-Hans" version="3.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net452" />
<package id="Microsoft.AspNet.Web.Optimization.zh-Hans" version="1.1.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Client.zh-Hans" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Core.zh-Hans" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.HelpPage" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Owin" version="5.2.3" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.WebHost.zh-Hans" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebPages.zh-Hans" version="3.2.3" targetFramework="net452" />
<package id="Microsoft.Bcl" version="1.1.10" targetFramework="net452" />
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net452" />
<package id="Microsoft.Bcl.Build" version="1.0.21" targetFramework="net452" />
<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.3" targetFramework="net452" />
<package id="Microsoft.Net.Compilers" version="1.3.2" targetFramework="net452" developmentDependency="true" />
<package id="Microsoft.Owin" version="3.1.0" targetFramework="net452" />
<package id="Microsoft.Owin.FileSystems" version="3.0.1" targetFramework="net452" />
<package id="Microsoft.Owin.Host.SystemWeb" version="3.0.1" targetFramework="net452" />
<package id="Microsoft.Owin.Security" version="3.1.0" targetFramework="net452" />
<package id="Microsoft.Owin.Security.Cookies" version="3.0.1" targetFramework="net452" />
<package id="Microsoft.Owin.Security.OAuth" version="3.0.1" targetFramework="net452" />
<package id="Microsoft.Owin.StaticFiles" version="3.0.1" targetFramework="net452" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net452" />
<package id="Modernizr" version="2.8.3" targetFramework="net452" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net452" />
<package id="Nito.AsyncEx" version="4.0.1" targetFramework="net452" />
<package id="Owin" version="1.0" targetFramework="net452" />
<package id="Respond" version="1.2.0" targetFramework="net452" />
<package id="Swashbuckle.Core" version="5.5.3" targetFramework="net461" />
<package id="System.Collections.Immutable" version="1.3.1" targetFramework="net452" />
<package id="System.Linq.Dynamic" version="1.0.7" targetFramework="net452" />
<package id="WebGrease" version="1.6.0" targetFramework="net452" />
</packages>

新建一个'AbpProjectTemplateWebAppModule.cs' 的类文件:

  [DependsOn(
typeof(AbpWebMvcModule),
typeof(AbpZeroOwinModule),
typeof(AbpProjectTemplateDataModule),
typeof(AbpProjectTemplateWebAppApiModule), typeof(AbpProjectTemplateApplicationModule) )]
public class AbpProjectTemplateWebAppModule : AbpModule
{
public override void PreInitialize()
{ }
public override void Initialize()
{
//进行以来注入
IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly()); IocManager.IocContainer.Register(
Component
.For<IAuthenticationManager>()
.UsingFactoryMethod(() => HttpContext.Current.GetOwinContext().Authentication)
.LifestyleTransient()
);
//Areas
AreaRegistration.RegisterAllAreas(); //Routes
RouteConfig.RegisterRoutes(RouteTable.Routes); //Bundling
BundleTable.Bundles.IgnoreList.Clear();
BundleConfig.RegisterBundles(BundleTable.Bundles);
} }

对Global.asax文件进行修改,修改后:

    public class WebApiApplication : AbpWebApplication<AbpProjectTemplateWebAppModule>
{
protected override void Application_Start(object sender, EventArgs e)
{
//Use UTC clock. Remove this to use local time for your application. //Log4Net configuration
AbpBootstrapper.IocManager.IocContainer
.AddFacility<LoggingFacility>(f => f.UseAbpLog4Net()
.WithConfig("log4net.config")
); base.Application_Start(sender, e);
} }

不要尝试在webapp中,直接引入abp.webapi。我失败了两次,但是之前的0.97版本又是可以的。。

然后创建一个webappapi的类库:

引入如下的nuge包

这个是webAppApi项目中的package.config:

<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Abp" version="1.5.1" targetFramework="net452" />
<package id="Abp.AutoMapper" version="1.5.1" targetFramework="net452" />
<package id="Abp.Web" version="1.5.1" targetFramework="net452" />
<package id="Abp.Web.Api" version="1.5.1" targetFramework="net452" />
<package id="Abp.Web.Common" version="1.5.1" targetFramework="net452" />
<package id="Abp.Zero" version="1.5.1" targetFramework="net452" />
<package id="Abp.Zero.Owin" version="1.5.1" targetFramework="net452" />
<package id="AutoMapper" version="5.2.0" targetFramework="net452" />
<package id="Castle.Core" version="3.3.3" targetFramework="net461" />
<package id="Castle.LoggingFacility" version="3.4.0" targetFramework="net452" />
<package id="Castle.Windsor" version="3.4.0" targetFramework="net452" />
<package id="JetBrains.Annotations" version="10.4.0" targetFramework="net452" />
<package id="Microsoft.AspNet.Identity.Core" version="2.2.1" targetFramework="net452" />
<package id="Microsoft.AspNet.Identity.Owin" version="2.2.1" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Owin" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.Bcl" version="1.1.10" targetFramework="net452" />
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net452" />
<package id="Microsoft.Bcl.Build" version="1.0.21" targetFramework="net452" />
<package id="Microsoft.Owin" version="3.1.0" targetFramework="net461" />
<package id="Microsoft.Owin.Security" version="3.1.0" targetFramework="net461" />
<package id="Microsoft.Owin.Security.Cookies" version="3.0.1" targetFramework="net461" />
<package id="Microsoft.Owin.Security.OAuth" version="3.0.1" targetFramework="net461" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net452" />
<package id="Nito.AsyncEx" version="4.0.1" targetFramework="net452" />
<package id="Owin" version="1.0" targetFramework="net452" />
<package id="Swashbuckle.Core" version="5.5.3" targetFramework="net452" />
<package id="System.Collections.Immutable" version="1.3.1" targetFramework="net452" />
<package id="System.Linq.Dynamic" version="1.0.7" targetFramework="net452" />
</packages>

然后下面就是创建一个"AbpProjectTemplateWebAppApiModule.cs"

 /// <summary>
/// 在系统中进行WebApi的配置
/// </summary>
[DependsOn(typeof(AbpWebApiModule), typeof(AbpProjectTemplateApplicationModule))]
public class AbpProjectTemplateWebAppApiModule : AbpModule
{ public override void Initialize()
{
IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
Configuration.Modules.AbpWebApi().DynamicApiControllerBuilder
.ForAll<IApplicationService>(typeof(AbpProjectTemplateApplicationModule).Assembly, "app")
.Build();
Configuration.Modules.AbpWebApi().HttpConfiguration.Filters.Add(new HostAuthenticationFilter("Bearer"));
}
}

然后就是配置EnableSwaggerUi了。这里的教程,请去看:ABP教程-给项目添加SwaggerUI,生成动态webapi

跑起来了后的截图:

[ABP框架]动态web Api的拦截用法。

接口太多了。那么我们就来折腾下

首先在application层创建一个

  /// <summary>
/// 使用该特性进行app过滤
///
/// </summary>
public class WebAppApiAttribute:System.Attribute
{ }

对AbpProjectTemplateWebAppApiModule中的配置方法进行改造下

Configuration.Modules.AbpWebApi().DynamicApiControllerBuilder
.ForAll<IApplicationService>(Assembly.GetExecutingAssembly(), "app")
.ForMethods(builder =>
{
if (!a.Method.IsDefined(typeof(WebAppApiAttribute)))
{
a.DontCreate = true;
}
})
.Build();

然后在特定的 方法上打上标记。

 public interface ICachingAppService : IApplicationService
{
[WebAppApi]
ListResultDto<CacheDto> GetAllCaches(); Task ClearCache(EntityDto<string> input);
[WebAppApi]
Task ClearAllCaches();
}

然后我们运行项目,看到如下结果:

[ABP框架]动态web Api的拦截用法。

更多的玩法,请参考官方文档:

Dynamic Web API Layer

https://aspnetboilerplate.com/Pages/Documents/v1.5.2/Dynamic-Web-API