core路由设置

全局路由设置

app.UseMvc(routes =>
            {
                routes.MapRoute( 
                    name: "areas",
                    template: "{area:exists}/{controller=Login}/{action=Index}/{id?}");

                routes.MapRoute( 
                    name: "default",
                    template: "{controller=Login}/{action=Index}/{id?}");
            });

区域路由控制器上加

[Area("FrontDesk")]
    public class LoginController : Controller
    {
        // GET: /<controller>/
        public IActionResult Index()
        {
            return View();
        }
    }

API路由

    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SysLogController : Controller
    {
    }

自己定义中间件定义路由参考

博客地址:https://blog.csdn.net/KingCruel/article/details/89228952

中间件定义

using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.Routing;
using System.Linq;
 
namespace Web.Api.Intercept
{
    /// <summary>
    /// api 路由拦截器(第一步)
    /// </summary>
    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;
                    }
                }
            }
        }
    }
}
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;
 
namespace Web.Api.Intercept
{
    /// <summary>
    /// api 路由拦截器(第二步)
    /// 扩展了MVCoptions
    /// </summary>
    public static class MvcOptionsExtensions
    {
        /// <summary>
        /// 扩展方法
        /// </summary>
        /// <param name="opts"></param>
        /// <param name="routeAttribute">自定的前缀内容</param>
        public static void UseCentralRoutePrefix(this MvcOptions opts, IRouteTemplateProvider routeAttribute)
        {
            // 添加我们自定义 实现IApplicationModelConvention的RouteConvention
            opts.Conventions.Insert(0, new RouteConvention(routeAttribute));
        }
 
    }
}

使用

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);
 
        }

 

posted @ 2019-09-17 15:10  世人皆萌  阅读(1201)  评论(0编辑  收藏  举报