ABP学习笔记

1.引导Abp系统

2.AbpSwaggerDemo

3.加密类

4.忽略webapi中的某个方法

5.Http动词

1.引导Abp系统

using System;
using Abp;
using Abp.Dependency;
using Castle.Facilities.Logging;

namespace AbpEfConsoleApp
{
    public class Program
    {
        static void Main(string[] args)
        {
            //引导ABP系统
            using (var bootstrapper = AbpBootstrapper.Create<MyConsoleAppModule>())
            {
                bootstrapper.IocManager
                    .IocContainer
                    .AddFacility<LoggingFacility>(f => f.UseLog4Net().WithConfig("log4net.config"));

                bootstrapper.Initialize();

                //从DI获取测试对象并运行它
                using (var tester = bootstrapper.IocManager.ResolveAsDisposable<Tester>())
                {
                    tester.Object.Run();
                } //部署测试和所有它的依赖

                Console.WriteLine("Press enter to exit...");
                Console.ReadLine();
            }
        }
    }
}
using System.Reflection;
using Abp.EntityFramework;
using Abp.Modules;

namespace AbpEfConsoleApp
{
    //定义模块取决于AbpEntityFrameworkModule
    [DependsOn(typeof(AbpEntityFrameworkModule))]
    public class MyConsoleAppModule : AbpModule
    {
        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
        }
    }
}

2.AbpSwaggerDemo

using System.Reflection;
using Abp.Application.Services;
using Abp.Modules;
using Abp.WebApi;
using Abp.WebApi.Controllers.Dynamic.Builders;
using OtherApp.Application;

namespace AbpSwagger.Application.WebApi.WebApi
{
    [DependsOn(
        typeof(AbpWebApiModule),
        typeof(AbpSwaggerAppModule),
        typeof(OtherAppModule))]
    public class SwaggerWebApiModule : AbpModule
    {
        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());

            DynamicApiControllerBuilder
                .ForAll<IApplicationService>(typeof(AbpSwaggerAppModule).Assembly, "app")
                .WithConventionalVerbs()
                .Build();

            DynamicApiControllerBuilder
                .ForAll<IApplicationService>(typeof(OtherAppModule).Assembly, "app")
                .Build();
        }
    }
}

3.加密类

//AES对称加密
SimpleStringCipher.Instance.Encrypt(input.ConnectionString)
//实现密码哈希方法
new PasswordHasher().HashPassword(input.Password)

4.忽略webapi中的某个方法

复制代码
DynamicApiControllerBuilder
    .ForAll<IApplicationService>(typeof(ChargeStationApplicationModule).Assembly, "ChargeStationAPI")
    .Build();//告诉生成器为所有实现了IApplicationService接口的服务方法创建api控制器
DynamicApiControllerBuilder
    .For<ICityAppService>("ChargeStationAPI/City")
    .ForMethod("CreateCity").DontCreateAction()
    .Build();//忽略"ChargeStationAPI/City"的"CreateCity"方法
复制代码

5.Http动词

①使用WithVerb

DynamicApiControllerBuilder.For<ICityAppService>("ChargeStationAPI/City")
    .ForMethod("getCities").WithVerb(HttpVerb.Get)
    .Build();

②使用特性(需要添加Microsoft.AspNet.WebApi.Core Nuget包的引用)

复制代码
public interface ICityAppService:IApplicationService
{
    [HttpGet]
    GetCitiesOutput GetCities(GetCityInput input);
    [HttpPost]
    void UpdateCity(CityInput input);
}
复制代码

③使用restful风格

DynamicApiControllerBuilder
    .ForAll<IApplicationService>(typeof(ChargeStationApplicationModule).Assembly, "ChargeStationAPI")
    .WithConventionalVerbs()//根据方法名使用惯例HTTP动词,默认对于所有的action使用Post
    .Build();

九十九、api用法

                //_userManager.SetRoles方法的作用:变更角色(前提:用户表以已经创建)
                CheckErrors(await _userManager.SetRoles(user, input.RoleNames));
            //admin用户不能被删除(删除用户的同时会删除UserRole)
            await _userManager.DeleteAsync(user);

用户名只能包含字母或数字

原文地址:https://www.cnblogs.com/zd1994/p/7717260.html