av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

學習ASP.NETMVC路由的使用方法

這里,我們將學習到mvc中一個重要的特性即asp.net routing。這個模塊用來把用戶的請求映射到特定的mvc controller actions,看完這篇將可以知道標準路由表是怎么把請求映射到controller action的。

婺源網站建設公司創(chuàng)新互聯(lián)公司,婺源網站設計制作,有大型網站制作公司豐富經驗。已為婺源上千余家提供企業(yè)網站建設服務。企業(yè)網站搭建\外貿網站建設要多少錢,請找那個售后服務好的婺源做網站的公司定做!

使用默認ASP.NET MVC路由表

當創(chuàng)建好一個mvc應用后,應用已經被配置好可以用asp.net routing了。ASP.NET MVC路由在兩個地方被激發(fā)。

一、asp.net routing在web.config中被激活,有關route的配置有四個配置節(jié):system.web.httpModules , system.web.httpHandlers , system.webserver.modules, the system.webserver.handlers,刪除這些配置節(jié)時要小心,因為沒有它們routing將不再工作。

二、更重要的是,在global.asax文件中已經創(chuàng)建了路由表,global.asax是一個包含了asp.net應用生命周期內事件句柄的特殊文件。路由表在Application Start 事件中被創(chuàng)建。

 
 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using System.Web.Routing;  
  7.  
  8. namespace MvcApplication1  
  9. {  
  10.     // Note: For instructions on enabling IIS6 or IIS7 classic mode,   
  11.     // visit http://go.microsoft.com/?LinkId=9394801  
  12.  
  13.     public class MvcApplication : System.Web.HttpApplication  
  14.     {  
  15.         public static void RegisterRoutes(RouteCollection routes)  
  16.         {  
  17.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  18.  
  19.             routes.MapRoute(  
  20.                 "Default",                                              // Route name  
  21.                 "{controller}/{action}/{id}",                           // URL with parameters  
  22.                 new { controller = "Home", action = "Index", id = "" } // Parameter defaults  
  23.             );  
  24.  
  25.         }  
  26.  
  27.         protected void Application_Start()  
  28.         {  
  29.             RegisterRoutes(RouteTable.Routes);  
  30.         }  
  31.     }  
  32. }  

當程序運行后,Application_Start() 方法被調用,同時調用方法內的RegisterRoutes() 方法。這個方法創(chuàng)建了路由表。

默認的ASP.NET MVC路由表中只含有一個叫做default的路由。這個默認路由把url拆分成三個部分分別對應controller,action和View。

當請求URL:/Home/Index/3時,下列代碼執(zhí)行:HomeController.Index(3)。

如果不指定controller,則默認為Home,不指定Action則默認為Index,不指定參數則默認為空。

我們將通過例子來看一下到底默認路由是怎么把URl映射到controller和action的。想象我們在地址欄中輸入了如下URL:

/Home

由于默認action為Index,所以下面的方法被調用

 
 
 
  1. using System.Web.Mvc;  
  2.  
  3. namespace MvcApplication1.Controllers  
  4. {  
  5.     [HandleError]  
  6.     public class HomeController : Controller  
  7.     {  
  8.         public ActionResult Index(string id)  
  9.         {  
  10.             return View();  
  11.         }  
  12.     }  
  13. }  

這個方法接受一個字符串類型的id參數,上面的URL執(zhí)行了這個方法,傳入一個空的id。

由于mvc框架引用controller actions的方式,/Home同時也會觸發(fā)下列事件

 
 
 
  1. using System.Web.Mvc;  
  2.  
  3. namespace MvcApplication1.Controllers  
  4. {  
  5.     [HandleError]  
  6.     public class HomeController : Controller  
  7.     {  
  8.         public ActionResult Index()  
  9.         {  
  10.             return View();  
  11.         }  
  12.     }  
  13. }  

這個方法不接受參數,/Home將觸發(fā)這個Index(),同時/Home/Index/3也會調用這個方法,id參數將被忽略。

/Home也會與下面的方法匹配

 
 
 
  1. using System.Web.Mvc;  
  2.  
  3. namespace MvcApplication1.Controllers  
  4. {  
  5.     [HandleError]  
  6.     public class HomeController : Controller  
  7.     {  
  8.         public ActionResult Index(int? id)  
  9.         {  
  10.             return View();  
  11.         }  
  12.     }  
  13. }  

該方法接受一個int類型的參數id,因為這個參數可以為null,該方法將被調用而不會產生任何錯誤。

***調用下面的方法,這里會引發(fā)一個異常

 
 
 
  1. using System.Web.Mvc;  
  2.  
  3. namespace MvcApplication1.Controllers  
  4. {  
  5.     [HandleError]  
  6.     public class HomeController : Controller  
  7.     {  
  8.         public ActionResult Index(int id)  
  9.         {  
  10.             return View();  
  11.         }  
  12.     }  
  13. }  

因為這個參數是不可以為null的。以上就介紹了ASP.NET MVC路由的使用方法。

【編輯推薦】

  1. 淺析ASP.NET中的URL Rewrite
  2. 淺談ASP.NET MVC框架
  3. 介紹ASP.NET MVC中的MvcAjaxPanel
  4. ASP.NET MVC框架拯救UpdatePanel
  5. 用ASP.NET MVC源代碼尋找解決方案

文章題目:學習ASP.NETMVC路由的使用方法
網頁路徑:http://uogjgqi.cn/article/dhgdede.html
掃二維碼與項目經理溝通

我們在微信上24小時期待你的聲音

解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯(lián)網交流