【发布时间】:2019-10-11 12:08:47
【问题描述】:
旧版 .net 核心框架使用 UseMvc() 添加全局路由前缀。如何在没有UseMvc() 的情况下为asp.net core 3 制作它?
【问题讨论】:
-
Asp.net core 3.0使用
app.UseEndpoints(),你要实现什么全局路由前缀?
标签: asp.net-core routes
旧版 .net 核心框架使用 UseMvc() 添加全局路由前缀。如何在没有UseMvc() 的情况下为asp.net core 3 制作它?
【问题讨论】:
app.UseEndpoints(),你要实现什么全局路由前缀?
标签: asp.net-core routes
我在 3.1 中通过我的启动 Configure() 中的以下内容解决了这个问题:
app.UsePathBase(new PathString("/api"));
【讨论】:
UseRouting() 中间件之前添加它,它在核心 3.1 中对我有用。如果之后添加,它将无法正常工作。
你可以参考下面asp.net core 3.0中的demo来设置全局路由前缀和api版本。你可以通过更改services.AddControllersWithViews(o => { o.UseGeneralRoutePrefix("api/v{version:apiVersion}"); });来设置任何你喜欢的前缀
1.创建自定义MvcOptionsExtensions
public static class MvcOptionsExtensions
{
public 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;
}
}
}
}
2.在Startup.cs中注册(需要安装包Microsoft.AspNetCore.Mvc.Versioning,当前3.0版本为4.0.0-preview8.19405.7)
public void ConfigureServices(IServiceCollection services) {
//MVC service registration
//https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.0&tabs=visual-studio#mvc-service-registration
services.AddControllersWithViews(o = >{
o.UseGeneralRoutePrefix("api/v{version:apiVersion}");
});
services.AddApiVersioning(o = >o.ReportApiVersions = true);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints = >{
endpoints.MapControllerRoute(
name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
}
3.控制器:
[ApiVersion("1")]
[ApiVersion("2")]
[Route("test")]
[ApiController]
public class TestController : ControllerBase
{
[HttpGet("version"), MapToApiVersion("1")]
public IActionResult GetV1()
{
return new OkObjectResult("Version One");
}
}
4.结果
调用/api/v1/test/version 会得到“版本一”。
【讨论】:
正如@alastairtree 回答的那样,您可以使用app.UsePathBase 来实现这一点。
您需要在注册需要前缀的中间件之前进行此调用。
如果之后需要注册其他中间件,不应该加前缀,可以将前缀重置为/。
完整示例:
app.UsePathBase(new PathString("/api"));
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
});
app.UsePathBase(new PathString("/"));
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});
【讨论】:
UsePathBase 的缺点似乎是前缀和非前缀路径都会导致端点。这可能是也可能不是问题,具体取决于您的用例。我个人转而为每个控制器添加前缀:[Route("api/[controller]")]