当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net core 系列 7 Razor框架路由(上)

asp.net core 系列 7 Razor框架路由(上)

2019年01月15日  | 移动技术网IT编程  | 我要评论

现代平民宗师传奇,喜嫁 琴律,成都房价

一.概述

  在上二篇中,主要是介绍了asp.net core mvc中路由的使用,这篇继续介绍路由在asp.net core razor中的使用。razor pages应该使用默认的传统路由,从应用程序的pages文件夹中提供命令资源。还可以使用其他约定来自定义 razor pages 路由行为。

  在asp.net core mvc 中是使用路由中间件来匹配传入请求的 url 并将它们映射到操作(action)。而asp.net core  razor使用页面路由和应用模型提供程序约定,来控制 razor 页面应用中的页面路由、发现和处理。

  使用addrazorpagesoptions 扩展方法向 startup 类中服务集合的 addmvc 服务中添加和配置 razor 页面约定。

            services.addmvc()
                .setcompatibilityversion(compatibilityversion.version_2_2)
                .addrazorpagesoptions(options=> {

                     //添加razor页面路由和应用模型约定
                    //options.conventions.add();
                });

  在asp.net core 的 razor框架页面中,路由和应用的约定有四大类。都需要实现ipageconvention接口。在mvc框架下路由需要实现iroutebuilder接口。

 

  (1) 模型约定 conventions.add

    通过conventions.add添加的模型约定(model conventions)。作用是:将路由模板和标头(page header)添加到应用的页面。模型约定有三种实现的接口ipageroutemodelconvention(路由模型约定)、ipageapplicationmodelconvention(应用模型约定)、ipagehandlermodelconvention(处理程序模型约定)。

 

  (2)  页面路由操作约定  page route action conventions

    通过页面路由操作约定。作用是:可以将路由模板添加到某个文件夹中的页面以及单个页面。addfolderroutemodelconvention(文件夹路由模型约定)、addpageroutemodelconvention(页面路由模型约定)addpageroute(配置页面路由)

 

  (3) 页面模型操作约定 page model action conventions

    通过页面模型操作约定。作用是:可以将标头添加到某个文件夹中的多个页面,将标头添加到单个页面,以及配置筛选器工厂以将标头添加到应用的页面  

addfolderapplicationmodelconvention(文件夹应用模型约定) addpageapplicationmodelconvention(页面应用模型约定) configurefilter(配置筛选器)

 

  (4) 默认页面应用模型提供程序 default page app model provider

    用户可以从默认模型提供程序继承,以便为处理程序发现和处理提供自己的实现逻辑 。

二. 模型约定

  为ipageconvention添加委托,以添加应用于 razor 页面的模型约定。

 

  2.1  ipageroutemodelconvention

    将路由模型约定添加到所有页面。使用约定创建ipageroutemodelconvention并将其添加到ipageconvention实例集合中,这些实例将在页面路由模型构造过程中应用。下面示例应用将 {globaltemplate?} 路由模板添加到应用中的所有页面。

    /// <summary>
    /// 只在程序启动时调用(每页面路由对应执行一次apply)
    /// </summary>
    public class globaltemplatepageroutemodelconvention : ipageroutemodelconvention
    {
        ///<summary>
        ///运用到所有页面路由模型中,制定页面路由模板,比如访问index页。
        ///路由模板可以是/index 也可以是/index/{可选参数}
        ///</summary>
        ///<param name="model"></param>
        public void apply(pageroutemodel model)
        {
            var selectorcount = model.selectors.count;
            for (var i = 0; i < selectorcount; i++)
            {
                var selector = model.selectors[i];
                model.selectors.add(new selectormodel
                {
                    attributeroutemodel = new attributeroutemodel
                    {
                        //执行路由顺序
                        order = 1,
                        //页面路由模板
                        template = attributeroutemodel.combinetemplates(selector.attributeroutemodel.template,"{globaltemplate?}")
                    }
                });

            }
        }
    }

    将 mvc 添加到startup.configureservices中的服务集合时,会添加 razor 页面选项,例如:添加上面的约定。

            services.addmvc()
                    .addrazorpagesoptions(options =>
                    {
                        options.conventions.add(new globaltemplatepageroutemodelconvention());
                    })
                    .setcompatibilityversion(compatibilityversion.version_2_1);

    在about页绑定 @routedata.values["globaltemplate"]。如果在浏览器访问: /about/globalroutevalue时,页面显示:route data for'globaltemplate'wasprovided:globalroutevalue

    在上面案例的路由模型约定的pageroutemodel实例中,在调试下查看about的路由规则生成如下:  model.selectors[1].attributeroutemodel.template的值为:

    about/{globaltemplate?}

 

  2.2 ipageapplicationmodelconvention

    将应用模型约定添加到所有页面。使用约定创建ipageapplicationmodelconvention并将其添加到ipageconvention实例集合中,这些实例将在页面应用模型构造过程中应用

    为了演示此约定,示例应用包含了一个 addheaderattribute 类。 类构造函数采用 name 字符串和 values 字符串数组。 将在其 onresultexecuting 方法中使用这些值来设置响应标头。使用 addheaderattribute 类将标头 globalheader 添加到应用中的所有页面。

    ///<summary>
    ///页面加载时调用(每一个路由地址)
    ///</summary>
    public class globalheaderpageapplicationmodelconvention : ipageapplicationmodelconvention
    {
        public void apply(pageapplicationmodel model)
        {
            model.filters.add(new addheaderattribute(
            "globalheader", new string[] { "global header value" }));
        }
    }

    public class addheaderattribute : resultfilterattribute
    {
        private readonly string _name;
        private readonly string[] _values;

        public addheaderattribute(string name, string[] values)
        {
            _name = name;
            _values = values;
        }

        public override void onresultexecuting(resultexecutingcontext context)
        {
            context.httpcontext.response.headers.add(_name, _values);
            base.onresultexecuting(context);
        }
    }
            services.addmvc()
                    .addrazorpagesoptions(options =>
                    {
                        options.conventions.add(new globaltemplatepageroutemodelconvention());
                        options.conventions.add(new globalheaderpageapplicationmodelconvention());
                    })
                    .setcompatibilityversion(compatibilityversion.version_2_1);

    在 /about 中请求示例的“关于”页面,并检查标头以查看结果:

 

  2.3 ipagehandlermodelconvention

     将处理程序模型约定添加到的所有页面。使用约定创建ipagehandlermodelconvention并将其添加到ipageconvention实例集合中,这些实例将在页面处理程序模型构造过程中应用

    public class globalpagehandlermodelconvention : ipagehandlermodelconvention
    {
        //页面加载时调用(每一个路由地址),在ipageapplicationmodelconvention约定之后执行
        public void apply(pagehandlermodel model)
        {
            //目前还不清楚能做什么
        }
    }
            services.addmvc()
                    .addrazorpagesoptions(options =>
                    {
                        options.conventions.add(new globaltemplatepageroutemodelconvention());
                        options.conventions.add(new globalheaderpageapplicationmodelconvention());
                        options.conventions.add(new globalpagehandlermodelconvention());
                    })
                    .setcompatibilityversion(compatibilityversion.version_2_1);

    

三. 页面路由操作约定

  默认路由模型提供程序派生自ipageroutemodelprovider,可调用旨在为页面路由配置提供扩展点的约定。   

 

  3.1 addfolderroutemodelconvention

     使用addfolderroutemodelconvention创建并添加ipageroutemodelconvention,后者可以为指定文件夹下的所有页面调用pageroutemodel上的操作。示例应用使用  

    addfolderroutemodelconvention 将 {otherpagestemplate?} 路由模板添加到 otherpages 文件夹中的页面:

                        options.conventions.addfolderroutemodelconvention("/otherpages", model =>
                        {
                            //otherpages文件夹下的页面,都用此路由模板。
                            var selectorcount = model.selectors.count;
                            for (var i = 0; i < selectorcount; i++)
                            {
                                var selector = model.selectors[i];
                                model.selectors.add(new selectormodel
                                {
                                    attributeroutemodel = new attributeroutemodel
                                    {
                                        //用于处理路由匹配,指定路由处理顺序。按顺序处理的路由 (-1、 0、 1、 2、 … n)
                                        order = 2,
                                        template = attributeroutemodel.combinetemplates
                                        (selector.attributeroutemodel.template,"{otherpagestemplate?}")
                                    }
                                });
                            }
                        });

  这里将attributeroutemodel的 order 属性设置为 2。在第一个路由数据值时(如:/page1/routedatavalue,这里的routedatavalue就是第一个路由数据值),可以保证之前模板{globaltemplate?}分配到优先级。

  当globaltemplate路由模板order=1,otherpagestemplate路由模板order=2时。在浏览器输入 :/otherpages/page1/routedatavalue。路由值由globaltemplate模板取出。

    routedata.values["otherpagestemplate"]
    null
    routedata.values["globaltemplate"]
    "routedatavalue"

 

  3.2 addpageroutemodelconvention

    使用addpageroutemodelconvention创建并添加ipageroutemodelconvention,后者可以为具有指定名称的页面调用pageroutemodel上的操作。

    示例应用使用addpageroutemodelconvention将 {abouttemplate?} 路由模板添加到“about”页面:

                        options.conventions.addpageroutemodelconvention("/about", model =>
                        {
                            //about页面,用此路由模板。
                            var selectorcount = model.selectors.count;
                            for (var i = 0; i < selectorcount; i++)
                            {
                                var selector = model.selectors[i];
                                model.selectors.add(new selectormodel
                                {
                                    attributeroutemodel = new attributeroutemodel
                                    {
                                        order = 2,
                                        template = attributeroutemodel.combinetemplates
                                        (selector.attributeroutemodel.template, "{abouttemplate?}")
                                    }
                                });
                            }
                        });

    这个功能就不在演示,功能与示例3.1相同,一个作用于文件夹下的所有pages页面,一个作用于指定某个page页面。重点关注三点:1是路由作用域,2是order路由顺序,3是定义好template路由规则。

    

  

 参考文献

  官方资料:

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网