文件目录
创建 RouteConvention
``` using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.ApplicationModels; using Microsoft.AspNetCore.Mvc.Routing;
namespace Cari.Rescue.Web.Common { ///
var matchedSelectors = controller.Selectors.Where(x => x.AttributeRouteModel != null).ToList();
if (matchedSelectors.Any())
{
foreach (var selectorModel in matchedSelectors)
{
// 在 当前路由上 再 添加一个 路由前缀
selectorModel.AttributeRouteModel = AttributeRouteModel.CombineAttributeRouteModel(_centralPrefix,
selectorModel.AttributeRouteModel);
}
}
//2、 没有标记 RouteAttribute 的 Controller
var unmatchedSelectors = controller.Selectors.Where(x => x.AttributeRouteModel == null).ToList();
if (unmatchedSelectors.Any())
{
foreach (var selectorModel in unmatchedSelectors)
{
// 添加一个 路由前缀
selectorModel.AttributeRouteModel = _centralPrefix;
}
}
}
}
}
} ```
创建 MvcOptionsExtensions
``` using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Cari.Rescue.Web.Common; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Routing;
namespace Router.Utility.Route { ///
添加配置
builder .Services.AddControllers(option => { //设置路由全局前缀 api option.UseCentralRoutePrefix(new RouteAttribute("api/")); }) .AddJsonOptions(option => { // 解决中文乱码 option.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All); }) ;