ASP.NET MVC Routing概述 C#描述

來源:互聯網
上載者:User

ASP.NET Routing模組的責任是將傳入的瀏覽器請求映射為特有的MVC controller actions。

使用 預設的Route Table

當你建立一個新的ASP.NET MVC應用程式,這個應用程式已經被配置用來使用ASP.NET Routing。 ASP.NET Routing 在2個地方設定。第一個,ASP.NET Routing 在你的應用程式中的Web設定檔( Web.config檔案)是有效。在設定檔中有4個與routing相關的程式碼片段:system.web.httpModules程式碼片段 ,system.web.httpHandlers 程式碼片段,system.webserver.modules程式碼片段以及 system.webserver.handlers代 碼段。千萬注意不要刪除這些程式碼片段,如果沒有這些程式碼片段,routing將不再運行。第二個,更重要的,route  table在應用程式的Global.asax檔案中建立。這個Global.asax檔案是一個特殊的檔案,它包含ASP.NET 應用程式生命週期events的event handlers。這個route  table在應用程式的起始event中創將。

在Listing 1中包含ASP.NET MVC應用程式的預設Global.asax檔案.

Listing 1 - Global.asax.cs

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 public class MvcApplication : System.Web.HttpApplication    {        public static void RegisterRoutes(RouteCollection routes)        {            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");            routes.MapRoute(                "Default", // 路由名稱                "{controller}/{action}/{id}", // 帶有參數的 URL                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 參數預設值            );            }            protected void Application_Start()        {            AreaRegistration.RegisterAllAreas();                RegisterRoutes(RouteTable.Routes);        }    }

當一個MVC應用程式第一個啟動,Application_Start() 方法被調用,這個方法反過來調用 RegisterRoutes() 方法。

這個預設的route  table包含一個單一的route。這個預設的route將 url的第一個段映射為一個controller名稱,url的第二個段映射為一個controller  action,第三個段映 射為命名為id的參數。

假如,你在網頁瀏覽器的地址欄中鍵入下面的url:/Home/Index/3,這個預設的 route將這個url映射為下面的參數:

controller = Home     controller名稱

action = Index          controller  action

id = 3                       id的參數

當你請 求/Home/Index/3這樣的url,下面的代碼將執行。HomeController.Index(3)

這個預設的route包含3個 預設的參數。如果你沒有提供一個 controller,那麼 controller預設為Home。同樣,action預設為Index,id 參數預設為空白字串。

讓我們來看一些關於預設的route怎麼映射urls為controller  actions的例 子。假如你在你的瀏覽器地址欄中輸入如下的url:/Home, 由於這些預設的route參數有一些相關的預設值, 鍵入這樣的URL,將導致HomeController類的Index()方法(如Listing 2)被調用。

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 namespace MvcRoutingApp.Controllers{    [HandleError]    public class HomeController : Controller    {        public ActionResult Index(string id)        {            ViewData["Message"] = "歡迎使用 ASP.NET MVC!";                return View();        }            public ActionResult About()        {            return View();        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.