zoukankan      html  css  js  c++  java
  • ASP.NET MVC(三)

    ASP.NET Routing 模块的责任是将传入的浏览器请求映射为特有的MVC controller actions。

    1. 请求 URL

    当我们不使用路由的时候 请求 http://server/application/Produt.aspx?id=4, 服务器会请求 Produt.aspx页面并携带一个名为id 的参数。

    当使用路由的时候,发出同样的请求时,不能确定会存在Produt.aspx页面。例如,请求http://server/application/Products/show/beverages,路由解析器对 Products  show beverages 进行处理。如果路由依据路径规则  server/applicaton/{area}/{action}/{category},就会发现,Productes是 area,它是控制器,action 对应的是 show ,毫无疑问 beverages 是categrory,这样就能正确显示页面。

         2. 配置 Route

    当创建新的ASP.NET MVC应用程序,这个应用程序被配置 Routing 有两个地方。

      2.1       web.config

    在配置文件中有4个与routing相关的代码片段:system.web.httpModules代码段,system.web.httpHandlers 代码段,system.webserver.modules代码段以及 system.webserver.handlers代码段。千万注意不要删除这些代码段,如果没有这些代码段,routing将不再运行。

      2.2   Global.asax

    它包含ASP.NET 应用程序生命周期events的event handlers。这个route  table在应用程序的起始event中创将。

            默认的route 我就不在这里浪费大家时间了,so easy。 接下来我们看看 如何自定义 Routes

      3.  Routing  简述

    在一个route中,通过在大括号中放一个占位符来定义( { and } )。当解析URL的时候,符号"/"和"."被作为一个定义符来解析,而定义符之间的值则匹配到占位符中。route定义中不在大括号中的信息则作为常量值。

    Route definition

    Example of matching URL

    {controller}/{action}/{id}

    /Products/show/beverages

    {table}/Details.aspx

    /Products/Details.aspx

    blog/{action}/{entry}

    /blog/show/123

    {reporttype}/{year}/{month}/{day}

    /sales/2008/1/5

    {locale}/{action}

    /US/show

    {language}-{country}/{action}

    /en-US/show

      

     4. 自定义 Routes 

    在Global.asax 文件中,使用自定义的route来修改默认的route table。

    4.1       位置: 自定义的Routes 应放到 默认路由之前,忽略路由之后

    4.2       示例

     public static void RegisterRoutes(RouteCollection routes)
            {
                //忽略对 .axd 文件的路由,向访问 webForm一样直接去访问 .axd文件
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
                //自定义路由
                routes.MapRoute(
                    "Blog", // 路由名称
                    "{controller}/{action}/{id}", // 带有参数的 URL
                    new { controller = "Blog", action = "Index", id = UrlParameter.Optional } // 参数默认值
                );
    
                //默认路由
                routes.MapRoute(
                    "Default", // 路由名称
                    "{controller}/{action}/{id}", // 带有参数的 URL
                    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
                );
            }
    
            protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
    
                // 默认情况下对 Entity Framework 使用 LocalDB
                Database.DefaultConnectionFactory = new SqlConnectionFactory(@"Data Source=(localdb)v11.0; Integrated Security=True; MultipleActiveResultSets=True");
    
                RegisterGlobalFilters(GlobalFilters.Filters);
                //程序启动时,定义Route规则
                RegisterRoutes(RouteTable.Routes);
            }
    RegisterRoutes

    在程序启动的时候, Application_Start() 的时候, 注册路由表信息RegisterRoutes

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapPageRoute("",
            "Category/{action}/{categoryName}",
            "~/categoriespage.aspx",
            true,
            new RouteValueDictionary 
                {{"categoryName", "food"}, {"action", "show"}},
            new RouteValueDictionary 
                {{"locale", "[a-z]{2}-[a-z]{2}"},{"year", @"d{4}"}}
           );
    }
    RegisterRoutes

    参考文献: http://msdn.microsoft.com/en-us/library/cc668201.aspx

  • 相关阅读:
    pcs7 opc 连接问题
    nuget 多个程序引用同一个库时,当个这个库更新时,引用的程序都要跟新,否则会在运行时出错
    nuget update FileConflictAction
    a
    C#版本
    .NET Framework版本与CLR版本之间的关系
    c# 将一种数组类型转成另一种数组类型
    tfs 清除缓存,在需要时
    java枚举和仿枚举
    java反射的用法
  • 原文地址:https://www.cnblogs.com/Theladyflower/p/ASPMVC.html
Copyright © 2011-2022 走看看