您好,登錄后才能下訂單哦!
如何分析ABP設(shè)置管理,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
ABP的設(shè)置管理。
開始
首先定義了一個FileOptions類,其中包含了幾個配置,然后在需要的地方中注入IOptions就可以使用這些信息了。
當(dāng)然,模塊啟動時可以做一些配置修改,比如:
無論是配置文件還是這種代碼形式的配置,都是程序?qū)用娴男薷?有些配置不太適合這樣做,比如這里的AllowedMaxFileSize和AllowedUploadFormats,它們應(yīng)該在應(yīng)用界面上,可以讓管理員自行修改。下面就來改造一下程序。
定義設(shè)置
使用設(shè)置之前需要先定義它,不同的模塊可以擁有不同的設(shè)置。
modules\file-management\src\Xhznl.FileManagement.Domain\Settings\FileManagementSettingDefinitionProvider.cs:
public 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.Create(name);
}
}
以上代碼定了了2個配置:AllowedMaxFileSize和AllowedUploadFormats,設(shè)置了它們的默認值、名稱和詳細說明。因為本項目使用了EasyAbp的SettingUi模塊,所以會有一些Group1,Group2之類的字段,具體介紹可以參考Abp.SettingUi
使用設(shè)置
想讀取設(shè)置信息,只需注入ISettingProvider即可。因為父類ApplicationService中已經(jīng)注入,所以這里直接使用SettingProvider就好。獲取到配置,然后就可以做一些邏輯處理,比如判斷上傳文件的大小和格式是否合法:
public class FileAppService : FileManagementAppService, IFileAppService
{
......
[Authorize]
public virtual async Task CreateAsync(FileUploadInputDto input) 鄭州人流醫(yī)院http://www.xasg120.net/
{
var allowedMaxFileSize = await SettingProvider.GetAsync(FileManagementSettings.AllowedMaxFileSize);//kb
var 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"]);
}
......
}
}
前端設(shè)置界面:
下面可以隨便修改下設(shè)置,進行測試:
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。