幽梦紫曦

Razor自定义路由

电脑版发表于:2021/12/2 21:51
1、类继承路由对象IRouteConstraint实现一个路由逻辑
public class RouteConstraint : IRouteConstraint
    {
        public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
        {
            if (int.TryParse(values["is"].ToString(), out int id))
            {
                if (id % 2 == 0)
                {
                    return true;
                }
            }
            return false;
        }
    }
在中间件启动服务
services.Configure<RouteOptions>(a =>
{
                //如果url查询字符串为小写,将LowercaseUrls设置为true,默认为false;
                a.LowercaseUrls = true;
                //同时LowercaseQueryStrings也需要设置为true,默认为false
                a.LowercaseQueryStrings = true;
                //生成的url后面附加一个斜杠
                a.AppendTrailingSlash = true;
                a.ConstraintMap.Add("even", typeof(RouteConstraint));
            });


由于这里没有设置生成连接数据库所以直接会是视图文件


这里有个小问题不能设置于首页不然打不开

关于TNBLOG
TNBLOG,技术分享。技术交流:群号677373950
ICP备案 :渝ICP备18016597号-1
App store Android
精彩评论
{{item.replyName}}
{{item.content}}
{{item.time}}
{{subpj.replyName}}
@{{subpj.beReplyName}}{{subpj.content}}
{{subpj.time}}
猜你喜欢