ページ

2011年11月25日金曜日

◆ASP.NET MVC ルーティング

ルーティングはGlobal.asaxで定義されている。

  1. public class MvcApplication : System.Web.HttpApplication  
  2. {  
  3.     public static void RegisterRoutes(RouteCollection routes)  
  4.     {  
  5.         routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  6.   
  7.         routes.MapRoute(  
  8.             "Default", // ルート名  
  9.             "{controller}/{action}/{id}", // パラメーター付きの URL  
  10.             new { controller = "Home", action = "Index", id = UrlParameter.Optional } // パラメーターの既定値  
  11.         );  
  12.   
  13.     }  
  14.   
  15.     protected void Application_Start()  
  16.     {  
  17.         AreaRegistration.RegisterAllAreas();  
  18.   
  19.         RegisterRoutes(RouteTable.Routes);  
  20.     }  
  21. }  

RouteCollection型となっているので複数のルーティングを定義できるのだろう。
初期状態では”Default”というルーティングが定義されていて、
「コントローラー名/アクション名/パラメータ」でルーティングされるようだ。


ControllerにはHome、actionにはIndexが初期値として定義してあるので、例えば
「http://ホスト名/コントローラ名」でアクセスされたときにはIndexアクションが実行されてIndexページが返るという仕掛けなのだろう。

0 件のコメント:

コメントを投稿

私が最近チェックした記事