feat: Controller 统一添加api路由前坠
parent
ab9cd9f5b5
commit
32ea7191ee
|
|
@ -2,5 +2,5 @@
|
|||
|
||||
public class GetUserDto : PageBase
|
||||
{
|
||||
public string Keyword { get; set; }
|
||||
public string? Keyword { get; set; }
|
||||
}
|
||||
|
|
@ -8,7 +8,7 @@ using UserPointManagement.Model.Entities;
|
|||
namespace UserPointManagement.Web.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
[Route("[controller]")]
|
||||
public class UserController : ControllerBase
|
||||
{
|
||||
private readonly IUserService _userService;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,45 @@
|
|||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
||||
using Microsoft.AspNetCore.Mvc.Routing;
|
||||
|
||||
namespace UserPointManagement.Web;
|
||||
|
||||
public static class MvcOptionsExtensions
|
||||
{
|
||||
private static void UseGeneralRoutePrefix(this MvcOptions opts, IRouteTemplateProvider routeAttribute)
|
||||
{
|
||||
opts.Conventions.Add(new RoutePrefixConvention(routeAttribute));
|
||||
}
|
||||
|
||||
public static void UseGeneralRoutePrefix(this MvcOptions opts, string
|
||||
prefix)
|
||||
{
|
||||
opts.UseGeneralRoutePrefix(new RouteAttribute(prefix));
|
||||
}
|
||||
}
|
||||
|
||||
public class RoutePrefixConvention : IApplicationModelConvention
|
||||
{
|
||||
private readonly AttributeRouteModel _routePrefix;
|
||||
|
||||
public RoutePrefixConvention(IRouteTemplateProvider route)
|
||||
{
|
||||
_routePrefix = new AttributeRouteModel(route);
|
||||
}
|
||||
|
||||
public void Apply(ApplicationModel application)
|
||||
{
|
||||
foreach (var selector in application.Controllers.SelectMany(c => c.Selectors))
|
||||
{
|
||||
if (selector.AttributeRouteModel != null)
|
||||
{
|
||||
selector.AttributeRouteModel = AttributeRouteModel.CombineAttributeRouteModel(_routePrefix, selector.AttributeRouteModel);
|
||||
}
|
||||
else
|
||||
{
|
||||
selector.AttributeRouteModel = _routePrefix;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -30,6 +30,7 @@ namespace UserPointManagement.Web
|
|||
{
|
||||
services.AddControllers(options =>
|
||||
{
|
||||
options.UseGeneralRoutePrefix("/api");
|
||||
});
|
||||
services.AddEndpointsApiExplorer();
|
||||
services.AddSwaggerGen(options =>
|
||||
|
|
@ -92,7 +93,7 @@ namespace UserPointManagement.Web
|
|||
app.UseStaticFiles();
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapControllers();
|
||||
|
|
|
|||
Loading…
Reference in New Issue