1。asp.net mvc URL routing
- IIS determines the request should be handled by ASP.NET
- The MvcHandler uses IControllerFactory to obtain an instance of IController using the "controller" route data from the route {controller}/{action}/{id}.
public static void RegisterRoutes(RouteCollection routes)<br /> {<br /> routes.IgnoreRoute("{resource}.axd/{*pathInfo}");</p><p> routes.MapRoute(<br /> "Default", // Route name<br /> "{controller}/{action}/{id}", // URL with parameters<br /> new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults<br /> );</p><p> }</p><p> protected void Application_Start()<br /> {<br /> AreaRegistration.RegisterAllAreas();</p><p> RegisterRoutes(RouteTable.Routes);<br /> }
- The HomeController is found, and its Execute method is invoked
- The HomeController invokes the Index action
- The Index action adds some objects to the ViewData dictionary
public ActionResult Index()<br /> {<br /> ViewData["Message"] = "Welcome to ASP.NET MVC!";</p><p> return View();<br /> }
- ASP.NET renders the response to the browser.
<h2><%= Html.Encode(ViewData["Message"]) %></h2>
2.Controller兩種形式
public ActionResult Index()<br /> {<br /> ViewData.Add("text", "Hello World2");</p><p> return View();<br /> }
<h2><%=ViewData["text"]%></h2>
public ActionResult Index()<br /> {<br /> return View((object)"Hello World3");<br /> }
<h2><%=Model %> I'm strongly typed in a layout.</h2>