网站推广方案策划书2000,网站页面架构怎么写,注销公司要花多少费用,做原油看哪个网站点击上方蓝字小黑在哪里关注我吧定义设置使用设置前言上一篇介绍了ABP模块化开发的基本步骤#xff0c;完成了一个简单的文件上传功能。通常的模块都有一些自己的配置信息#xff0c;比如上篇讲到的FileOptions类#xff0c;其中配置了文件的上传目录#xff0… 点击上方蓝字小黑在哪里关注我吧定义设置使用设置前言上一篇介绍了ABP模块化开发的基本步骤完成了一个简单的文件上传功能。通常的模块都有一些自己的配置信息比如上篇讲到的FileOptions类其中配置了文件的上传目录允许的文件大小和允许的文件类型。配置信息可以通过Configuration[1]配置和Options[2]选项来完成ABP还提供了另一种更灵活的方式Settings[3]设置本篇就来介绍一下ABP的设置管理。开始回顾一下上篇的FileOptions首先定义了一个FileOptions类其中包含了几个配置然后在需要的地方中注入IOptionsFileOptions就可以使用这些信息了。当然模块启动时可以做一些配置修改比如无论是配置文件还是这种代码形式的配置都是程序层面的修改有些配置不太适合这样做比如这里的AllowedMaxFileSize和AllowedUploadFormats它们应该在应用界面上可以让管理员自行修改。下面就来改造一下程序。定义设置使用设置之前需要先定义它不同的模块可以拥有不同的设置。modules\file-management\src\Xhznl.FileManagement.Domain\Settings\FileManagementSettingDefinitionProvider.cspublic class FileManagementSettingDefinitionProvider : SettingDefinitionProvider
{public override void Define(ISettingDefinitionContext context){/* Define module settings here.* Use names from FileManagementSettings class.*/context.Add(new SettingDefinition(FileManagementSettings.AllowedMaxFileSize,1024,L(DisplayName:FileManagement.AllowedMaxFileSize),L(Description:FileManagement.AllowedMaxFileSize)).WithProperty(Group1, File).WithProperty(Group2, Upload).WithProperty(Type, number),new SettingDefinition(FileManagementSettings.AllowedUploadFormats,.jpg,.jpeg,.png,.gif,.txt,L(DisplayName:FileManagement.AllowedUploadFormats),L(Description:FileManagement.AllowedUploadFormats)).WithProperty(Group1, File).WithProperty(Group2, Upload).WithProperty(Type, text));}private static LocalizableString L(string name){return LocalizableString.CreateFileManagementResource(name);}
}
以上代码定了了2个配置AllowedMaxFileSize和AllowedUploadFormats设置了它们的默认值、名称和详细说明。因为本项目使用了EasyAbp的SettingUi模块所以会有一些Group1Group2之类的字段具体介绍可以参考Abp.SettingUi[4]使用设置想读取设置信息只需注入ISettingProvider即可。因为父类ApplicationService中已经注入所以这里直接使用SettingProvider就好。获取到配置然后就可以做一些逻辑处理比如判断上传文件的大小和格式是否合法public class FileAppService : FileManagementAppService, IFileAppService
{......[Authorize]public virtual async Taskstring CreateAsync(FileUploadInputDto input){var allowedMaxFileSize await SettingProvider.GetAsyncint(FileManagementSettings.AllowedMaxFileSize);//kbvar allowedUploadFormats (await SettingProvider.GetOrNullAsync(FileManagementSettings.AllowedUploadFormats))?.Split(,, StringSplitOptions.RemoveEmptyEntries);if (input.Bytes.Length allowedMaxFileSize * 1024){throw new UserFriendlyException(L[FileManagement.ExceedsTheMaximumSize, allowedMaxFileSize]);}if (allowedUploadFormats null || !allowedUploadFormats.Contains(Path.GetExtension(input.Name))){throw new UserFriendlyException(L[FileManagement.NotValidFormat]);}......}
}
前端设置界面下面可以随便修改下设置进行测试最后本篇内容较少希望对你有帮助。代码已上传至 https://github.com/xiajingren/HelloAbp 欢迎star。参考资料[1]Configuration: https://docs.abp.io/zh-Hans/abp/latest/Configuration[2]Options: https://docs.abp.io/zh-Hans/abp/latest/Options[3]Settings: https://docs.abp.io/zh-Hans/abp/latest/Settings[4]Abp.SettingUi: https://github.com/EasyAbp/Abp.SettingUi如果本文对您有用不妨点个“在看”或者转发朋友圈支持一下