ASP.NET Core API 配置全局路由

ASP.NET Web API 2 中的属性路由
1、先定义一个类,用来实现IApplicationModelConvention  接口。

using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.Routing;
using System.Linq;

namespace Web.Api.Intercept
{
    /// 
    /// api 路由拦截器(第一步)
    /// 
    public class RouteConvention : IApplicationModelConvention
    {
        private readonly AttributeRouteModel _centralPrefix;

        public RouteConvention(IRouteTemplateProvider routeTemplateProvider)
        {
            _centralPrefix = new AttributeRouteModel(routeTemplateProvider);
        }

        //接口的Apply方法
        public void Apply(ApplicationModel application)
        {
            //遍历所有的 Controller
            foreach (var controller in application.Controllers)
            {
                // 已经标记了 RouteAttribute 的 Controller
                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);
                    }
                }

                // 没有标记 RouteAttribute 的 Controller
                var unmatchedSelectors = controller.Selectors.Where(x => x.AttributeRouteModel == null).ToList();
                if (unmatchedSelectors.Any())
                {
                    foreach (var selectorModel in unmatchedSelectors)
                    {
                        // 添加一个 路由前缀
                        selectorModel.AttributeRouteModel = _centralPrefix;
                    }
                }
            }
        }
    }
}

2、定义一个类,插入路由

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;

namespace Web.Api.Intercept
{
    /// 
    /// api 路由拦截器(第二步)
    /// 扩展了MVCoptions
    /// 
    public static class MvcOptionsExtensions
    {
        /// 
        /// 扩展方法
        /// 
        /// 
        /// 自定的前缀内容
        public static void UseCentralRoutePrefix(this MvcOptions opts, IRouteTemplateProvider routeAttribute)
        {
            // 添加我们自定义 实现IApplicationModelConvention的RouteConvention
            opts.Conventions.Insert(0, new RouteConvention(routeAttribute));
        }

    }
}

3、在startup.cs 里面ConfigureServices 方法添加配置信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Web.Api.Intercept;

namespace Web.Api
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvcCore()
               .AddAuthorization()
               .AddJsonFormatters();

            //默认
            //services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            services.AddMvc(opt =>
            {
                #region 配置全局路由(第三步)
                //在各个控制器添加前缀(没有特定的路由前面添加前缀)
                opt.UseCentralRoutePrefix(new RouteAttribute("lg/v1/[action]"));
                //opt.UseCentralRoutePrefix(new RouteAttribute("api/[controller]/[action]"));
                #endregion

            }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            app.UseAuthentication();
            app.UseHttpsRedirection();

            app.UseMvc();
        }
    }
}

4、运行

原先控制器路由“前缀”保留,如下方式:

[Route("api/[controller]")] //https://localhost:44390/lg/v1/get/api/default
[ApiController]
public class DefaultController : ControllerBase
{
    // GET: api/Default
    [HttpGet]
    public IEnumerable Get()
    {
        return new string[] { "value3", "value4" };
    }
}

把原先的路由“前缀”去除,如下方式:

[Route("")]                   //https://localhost:44390/lg/v1/get
[ApiController]
public class DefaultController : ControllerBase
{
    // GET: api/Default
    [HttpGet]
    public IEnumerable Get()
    {
        return new string[] { "value3", "value4" };
    }
}

 

你可能感兴趣的:(.NET,Core)