流程:
一、MVC架構會以路由(Route)裡的設定 new一個設定中的controller
(這裡是HomeController)
(這裡是HomeController)
二、呼叫設定裡同名的action,接著Index()回傳view() 也就是同名的
index.cshtml,最後將結果顯示到畫面上
index.cshtml,最後將結果顯示到畫面上
也就是HomeController => Action(Index()) => 回傳View()
路由(Route): 其實在設定一開始的網址 (http://localhost:XXXX/Home/index)
APP_start/RouteConfig.cs
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}" );
//忽略.axd的檔案的意思
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { //這裡設定預設controller & View
controller = "Home",
action = "Index" ,
id = UrlParameter .Optional }
controller = "Home",
action = "Index" ,
id = UrlParameter .Optional }
);
}
}
Controllers/HomeController.cs
public ActionResult Index() //Index對應到 Views/Index.cshtml
{
return view(); =Views/Index.cshtml
}
沒有留言:
張貼留言