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

asp.net core 系列 8 Razor框架路由(下)

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

好上居,邮箱登陆126,周钦年

三.页面路由操作约定

   接着上篇讲asp.net core 系列 7 razor框架路由。在上篇继续第三节 "页面路由操作约定" 的最后一小节 addpageroute 。

 

  3.3. 配置页面路由addpageroute

    使用 addpageroute 配置路由,该路由与指定页面关联, 使用指定的路由生成页面链接。 addpageroute 使用 addpageroutemodelconvention 建立路由。

    示例应用为 privacy.cshtml 创建指向 /theprivacypage 的路由:

    options.conventions.addpageroute("/privacy", "theprivacypage/{text?}");

    可以通过原有 / privacy默认路由访问“privacy”页面。http://www.lhsxpumps.com/_localhost:60397/privacy

    也可以通过上面自定义的页面路由访问privacy页面。 http://localhost:60397/theprivacypage

    示例应用的“privacy”页面自定义路由允许使用可选的 text 路由段 ({text?})。 该页面还在其 @page 指令中包含此可选段,以便访问者在 /privacy 路由中访问该页面。在呈现的页面中,为privacy链接生成的 url 显示了已更新的路由,如下所示:

 

四. 页面模型操作约定

    实现 ipageapplicationmodelprovider 的默认页面模型提供程序可调用约定,这些约定旨在为页面模型配置提供扩展点。 在生成和修改页面发现及处理方案时,可使用这些约定。这里继续使用上篇讲的 addheaderattribute 类(一个resultfilterattribute)来应用响应标头。

    

  4.1 文件夹应用模型约定

    使用 addfolderapplicationmodelconvention 创建并添加 ipageapplicationmodelconvention,后者可以为指定文件夹下的所有页面调用 pageapplicationmodel 实例上的操作。   示例演示了如何使用 addfolderapplicationmodelconvention 将标头 otherpagesheader 添加到应用的otherpages 文件夹内的页面:

            //文件夹应用模型约定
                        options.conventions.addfolderapplicationmodelconvention("/otherpages", model =>
                        {
                            model.filters.add(new addheaderattribute(
                                "otherpagesheader", new string[] { "otherpages header value" }));
                        });

    在otherpages/page1 中请求示例的 page1 页面,并检查标头以查看结果:

 

  4.2 页面应用模型约定    

    使用addpageapplicationmodelconvention创建并添加ipageapplicationmodelconvention ,它在调用操作pageapplicationmodel页使用指定的名称。示例演示了如何使用 addpageapplicationmodelconvention 将标头 aboutheader 添加到“about”页面:

                    //页面应用模型约定
                        options.conventions.addpageapplicationmodelconvention("/privacy", model =>
                        {
                           
                            model.filters.add(new addheaderattribute(
                                "privacyheader", new string[] { "privacy header value" }));
                        });

    请求示例的 privacy页面,并检查标头以查看结果:

 

  4.3 配置筛选器

    configurefilter 可配置要应用的指定筛选器。 用户可以实现筛选器类,但示例应用演示了如何在 lambda 表达式中实现筛选器,该筛选器在后台作为可返回筛选器的工厂实现:

                        options.conventions.configurefilter(model =>
                        {
                            if (model.relativepath.contains("otherpages/page2"))
                            {
                                return new addheaderattribute(
                                    "otherpagespage2header",
                                    new string[] { "otherpages/page2 header value" });
                            }
                           return new pages.otherpages.emptyfilter();
                        });
    public class emptyfilter : iactionfilter
    {
        public void onactionexecuting(actionexecutingcontext context)
        {
            // do something before the action executes
        }

        public void onactionexecuted(actionexecutedcontext context)
        {
            // do something after the action executes
        }
    }

    页面应用模型用于检查指向 otherpages 文件夹中 page2 页面的段的相对路径。 如果条件通过,则添加标头。 如果不通过,则应用 emptyfilter。由于 razor 页面会忽略操作筛选器,因此,如果路径不包含 otherpages/page2emptyfilter 会按预期发出空操作指令。

    在otherpages/page2中请求示例的 page2 页面,并检查标头以查看结果:

   4.4 配置筛选器工厂 

    除了4.3的 lambda 表达式配置筛选器。还可以对configurefilter 配置指定的工厂,以将筛选器应用于所有 razor 页面。示例应用说明如何使用筛选器工厂将具有两个值的标头 filterfactoryheader 添加到应用的页面:

     options.conventions.configurefilter(new addheaderwithfactory());
public class addheaderwithfactory : ifilterfactory
    {
        // implement ifilterfactory
        public ifiltermetadata createinstance(iserviceprovider serviceprovider)
        {
            return new addheaderfilter();
        }

      /// <summary>
      /// iresultfilter继承了ifiltermetadata接口
      /// </summary>
     private class addheaderfilter : iresultfilter
        {
            public void onresultexecuting(resultexecutingcontext context)
            {
                context.httpcontext.response.headers.add(
                    "filterfactoryheader",
                    new string[]
                    {
                    "filter factory header value 1",
                    "filter factory header value 2"
                    });
            }

            public void onresultexecuted(resultexecutedcontext context)
            {
            }
        }

        public bool isreusable
        {
            get
            {
                return false;
            }
        }
    }

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

 

五.替换默认的页面应用模型

         razor 页面使用 ipageapplicationmodelprovider 接口创建 defaultpageapplicationmodelprovider。 用户可以从默认模型提供程序继承,以便为处理程序提供自己的实现逻辑。 默认实现是:“未命名的处理程序方法”和“默认已命名处理程序的约定方法。

  

  5.1 默认的未命名处理程序方法

    未命名处理程序方法是以:http 谓词为处理的程序方法,遵循以下约定:on<http verb>[async](追加 async 是可选操作,但建议为异步方法执行此操作)。主要的三个http 谓词:get、post、delete。

未命名处理程序方法

操作

onget/ongetasync

初始化页面状态

onpost/onpostasync

处理 post 请求。

ondelete/ondeleteasync

处理 delete 请求。

    例如在index页面,实现post提交,示例如下:

    <form method="post"  >
        <input type="submit"  value="新增"
                class="btn btn-danger"
                asp-route-id="1"  />
    </form>
       [httppost]
        public async task<iactionresult> onpostasync(int id)
        {
            await saveasync(id);
            //  redirecttopageresult实现了iactionresult接口
            redirecttopageresult result = redirecttopage();
            return result;
        }

 

  5.2 默认的已命名处理程序方法

   由开发人员提供的处理程序方法,遵循的约定是: on<http verb><handler name>[async],  处理程序名称出现在 http 谓词之后或者 http 谓词与 async 之间。 例如,提交一个处理程序方法名为message,那命名约定是onpostmessage/onpostmessageasync。

      <form method="post">
        <input type="submit" value="消息提交"
               class="btn btn-danger"
               asp-route-id="1" asp-page-handler="message" />
      </form>
        public async task<iactionresult> onpostmessageasync(int id)
        {
            await saveasync(id);
            return redirecttopage(); 
        }

    注意:onpostmessageasync上面不用加http谓词。在页面asp-page-handler必须指定后台处理程序方法名。

 

  5.3 自定义处理程序方法名称

    上面的处理程序方法都是需要按照默认约定,才能关联起来。使用自定义处理程序可以让用户更改未命名和已命名的程序方法的命名方式。 假设:避免让方法名称以“on”开头,并使用第一个分词来确定 http 谓词,比如将delete、put 和 patch 的谓词转换为 post。这样程序可以提供下表所示的方法名称。

处理程序方法

操作

get

初始化页面状态

post/postasync

处理 post 请求

postmessage/postmessageasync

post 消息

deletemessage/deletemessageasync

ost 消息以进行删除

putmessage/putmessageasync

post 消息以进行放置

    若要建立此方案,请从 defaultpageapplicationmodelprovider 类继承并重写 createhandlermodel 方法,以提供自定义逻辑来解析 pagemodel 处理程序名称。 示例应用展示了如何在其 custompageapplicationmodelprovider 类中执行此操作:

    当custompageapplicationmodelprovider类继承defaultpageapplicationmodelprovider想重写处理程序方法名称时,vs提示错误:defaultpageapplicationmodelprovider不可访问,因为它具有一定保护级别。保护级别如下图所示:

     在实际项目中,一般也不会自定义处理程序方法名称,遵循既有的方法名约定都能满足开发业务。这里的实现以后在考虑吧。

 

   

参考文献

官方资料:

 

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

相关文章:

验证码:
移动技术网