Net Core 伪静态配置

创建伪静态类
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using System.Text;

namespace Micro.Web
{
    public class PageRouteModelConvention : IPageRouteModelConvention
    {
        public void Apply(PageRouteModel model)
        {
            var selectorsCount = model.Selectors.Count;
            for (var i = 0; i < selectorsCount; ++i)
            {
                var attributeRouteModel = model.Selectors[i].AttributeRouteModel;
                if(attributeRouteModel!= null)
                {
                    //添加之前
                    if (string.IsNullOrEmpty(attributeRouteModel.Template)) continue;
                    //该规则是否禁止链接的生成,默认为生成(支持TagHelpers) asp-page="/Index" 
                    attributeRouteModel.SuppressLinkGeneration = true;
                    //添加新的路由模板
                    model.Selectors.Add(new SelectorModel
                    {
                        AttributeRouteModel = new AttributeRouteModel
                        {
                            //Order 路由匹配顺序
                            //SuppressLinkGeneration = true,
                            Template = $"{attributeRouteModel.Template}.html",
                        }
                    });
                }
            }
        }
    }
}

Program.cs 调用

builder.Services.AddRazorPages().AddRazorPagesOptions(options => options.Conventions.Add(new PageRouteModelConvention()));

 

posted @ 2023-12-11 08:42  microsoft-zhcn  阅读(66)  评论(0编辑  收藏  举报