xDM6.MvcBase
2.24.1.2401
dotnet add package xDM6.MvcBase --version 2.24.1.2401
NuGet\Install-Package xDM6.MvcBase -Version 2.24.1.2401
<PackageReference Include="xDM6.MvcBase" Version="2.24.1.2401" />
paket add xDM6.MvcBase --version 2.24.1.2401
#r "nuget: xDM6.MvcBase, 2.24.1.2401"
// Install xDM6.MvcBase as a Cake Addin #addin nuget:?package=xDM6.MvcBase&version=2.24.1.2401 // Install xDM6.MvcBase as a Cake Tool #tool nuget:?package=xDM6.MvcBase&version=2.24.1.2401
net480 / net6 下MVC的基础类库,提供全局过滤器 GlobalFilterBase,屏蔽两都差异
Mvc的一些基本东西
全局过滤器 GlobalFilterBase,用于处理 全局登陆验证、全局异常处理、全局Action执行过程/完毕、自定义过滤器
使用方法
1 继承并实现 xDM.MvcBase.Filters.GlobalFilterBase 下面为示例的伪代码
public class SBGlobalAuthorize : xDM.MvcBase.Filters.GlobalFilterBase
{
protected override bool CheckUserLogged(XFilterContext context, out object loginUser, out MvcResBase msg)
{
//这里检查登陆状态
loginUser = null;
msg = null;
return true;
}
protected override void OnException(XExceptionContext context)
{
//这里做全局异常处理,比如保存到文件
}
}
.Net Framework 4.8 + 启用所有请求支持,包括静态文件请求(如果需要)
<modules runAllManagedModulesForAllRequests="true">
2.1 .Net Framework 4.8 + 使用方法,修改 Global.asax 文件的 Application_Start() 方法
使用 use (推荐)
//this.UseNewtonjsonToApiDefaultFormatterWithDefaultSettings();
var gFilter = new GlobalFilter();
this.UseErrorHandler(globalFilter);
this.UseBeginRequestHandler(globalFilter, true);
this.UseXGlobalFilter(globalFilter);
this.UseEndRequestHandler(globalFilter);
this.RemoveDefaultResponseHeaders();
this.SetServerToResponseHeaders("UCDOS");
2.2 .Net6 使用方法,修改 Program.cs
使用 use (推荐)
builder.Services.AddControllersWithViewsUseXGlobalFilter(globalFilter);
//var app = builder.Build(); 后面添加
app.UseErrorHandler(globalFilter);
app.UseBeginRequestHandler(globalFilter);
/*其他代码*/
app.UseEndRequestHandler(globalFilter);
//app.Run(); 前面添加
或 在 builder.Build() 前面增加下面代码:
builder.Services.AddMvc(config => {
config.Filters.Add(new SBGlobalAuthorize());
});
3 全局 Filter 执行流程,可通过重写基类的相应方法来实现功能
-->>> 请求开始
-->> 【XGlobalFilterBase】BeginRequesttHandler
==>> 【XGlobalFilterBase】OnActionExecutingBeforeAuthorazing
if(!SkipCustomIxFiltersAttribute) ++>> 【Action/Controller的Attribute】 IxFilterGeneral.OnActionExecutingBeforeAuthorazing if(返回结果 == false) ==>> OnFilterExcuteFaild
if(!AllowAnonymous) ==>> 【XGlobalFilterBase】OnAuthorazing if(返回结果 == true) ==>> 【XGlobalFilterBase】OnAuthorizationPass else ==>> 【XGlobalFilterBase】OnAuthorizationNotPass
if(!SkipCustomIxFiltersAttribute) ++>> 【Action/Controller的Attribute】 IxFilterGeneral.OnActionExecutingAfterAuthorazed if(返回结果 == false) ==>> OnFilterExcuteFaild
==>> 【XGlobalFilterBase】OnActionExecutingAfterAuthorazed
**>> 【Controller】 return View() / OtherActionResult
if(!SkipCustomIxFiltersAttribute) ++>> 【Action/Controller的Attribute】 IxFilterGeneral.OnActionExecuted if(返回结果 == false) ==>> OnFilterExcuteFaild
==>> 【XGlobalFilterBase】OnActionExecuted
-->> 【XGlobalFilterBase】EndRequesttHandler
-->>> 请求结束
OnFilterExcuteFaild 在自定义过滤器 IxFilterAttributeBase 的 OnActionExecutingBeforeCheckLogin/OnActionExecutingAfterCheckLogin/OnActionExecuted 返回false时执行
OnActionException 在发生错误时执行,可以处理全局异常
自定义过滤器 xDM.MvcBase.Filters.IxFilterGeneral/xDM.MvcBase.Filters.XFilterAttributeBase
通过继承 XFilterAttributeBase 或 继承 System.Attribute 并实现 IxFilterGeneral 接口来为 Controller 或 Action 提供过滤特性
以下为示例
/// <summary>
/// 开启 CORS 支持
/// </summary>
public class EnableCORSAttribute : XFilterAttributeBase //或 public class EnabledCORSAttribute : System.Attribute, IxFilterGeneral
{
/// <summary>
/// 启用 CROS 支持
/// </summary>
/// <param name="allowCookies">开启携带cookie</param>
/// <param name="orign">指定域名访问</param>
public EnableCORSAttribute(bool enabled, bool allowCookies = true, string orign = "*")
{
Enabled = enabled;
AllowCookies = allowCookies;
Orign = orign;
}
public bool Enabled { get; }
public bool AllowCookies { get; set;}
public string Orign { get; set;}
public override bool OnActionExecuted(XFilterContext context, out MvcResBase msg)
{
msg = null;
context.HttpContext.Response.Headers.Add("Access-Control-Allow-Origin", Orign);
if (AllowCookies)
{
context.HttpContext.Response.Headers.Add("Access-Control-Allow-Credentials", "true");
}
return true; //返回 true 表示执行成功,false 表示执行失败
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Framework | net48 is compatible. net481 was computed. |
-
.NETFramework 4.8
- Microsoft.AspNet.Mvc (>= 5.2.7)
- Microsoft.AspNet.WebApi.Core (>= 5.2.7)
- xDM6 (>= 2.23.12.2001)
- xDM6.Http.Web (>= 1.23.11.803)
-
net6.0
- xDM6 (>= 2.23.12.2001)
- xDM6.Http.Web (>= 1.23.11.803)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.24.1.2401 | 93 | 1/23/2024 |
1.23.11.2301 | 106 | 12/11/2023 |