為了調研ASP.NET MVC是否適合在公司項目中應用,研究了一段時間。感覺網上資料中講實踐的比較少,我在這裡總結一下以備以後查用。
ASP.NET MVC 包含了一個強大的URL路由引擎,它允許我們自訂選擇使用哪個控制器類,根據不同的參數來控制調用哪個action方法。ASP.NET MVC 中有一套預設的規則來簡化控制類以及action方法的調用,如果不瞭解這個預設規則,在使用中比較容易讓人迷惑,我們結合ASP.NET MVC模板來簡單瞭解一下這些預設規則。當我們根據ASP.NET MVC的模板建立一個ASP.NET MVC應用時,我們可以在Global.asax檔案中找到以下代碼:
{
function onclick()
{
function onclick()
{
function onclick()
{
function onclick()
{
this.style.display='none'; document.getElementById('Code_Closed_Text_111423').style.display='none'; document.getElementById('Code_Open_Image_111423').style.display='inline'; document.getElementById('Code_Open_Text_111423').style.display='inline';
}
}
}
}
}" id="Code_Closed_Image_111423" style="display: none">{
function onclick()
{
function onclick()
{
function onclick()
{
function onclick()
{
this.style.display='none'; document.getElementById('Code_Open_Text_111423').style.display='none'; getElementById('Code_Closed_Image_111423').style.display='inline'; getElementById('Code_Closed_Text_111423').style.display='inline';
}
}
}
}
}" id="Code_Open_Image_111423">Code
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
我們知道Application_Start是在第一個請求訪問網站之前就被調用的,所以這個例子中的網站啟動的就已經註冊了一個路由網站系統中
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = "" } );方法的第一個參數是路由的名字,暫時用不到,假如映射多個路由的時候名字不能重複,第二個參數是Url的格式,第三個參數是個匿名對象,這個匿名對象所表達的是按照第二個參數格式所對應的預設的controller和action以及id是什麼,這裡我們用一個表格進一步解釋一下:
URL |
Controler類 |
Action方法 |
輸入參數 |
/Home/Index/5 |
HomeController |
Index(int id) |
5 |
/Home/Edit/5 |
HomeController |
Edit(int id) |
5 |
/Home/Index |
HomeController |
Index() |
無 |
/Home |
HomeController |
Index() |
無 |
/ |
HomeController |
Index() |
無 |
|
|
|
|
觀察這個表格我們可以發如果我們輸入:http//localhost:4804/Home/Index/5地址,那麼路由會調用HomeController的Index(5)的方法,而當輸入的地址為http//localhost:4804時,路由會根據匿名對象new { controller = "Home", action = "Index", id = "" }中的預設值來選取相應的Controller和action。
當我們建立第一個ASP.NET MVC應用時我們可以發現一個default.aspx,這個檔案其實是用來在網站部署時簡化IIS部署的,查看default.aspx.cs檔案我們可以看到以下代碼:
{
function onclick()
{
function onclick()
{
function onclick()
{
function onclick()
{
this.style.display='none'; document.getElementById('Code_Closed_Text_111455').style.display='none'; document.getElementById('Code_Open_Image_111455').style.display='inline'; document.getElementById('Code_Open_Text_111455').style.display='inline';
}
}
}
}
}" id="Code_Closed_Image_111455" style="display: none">{
function onclick()
{
function onclick()
{
function onclick()
{
function onclick()
{
this.style.display='none'; document.getElementById('Code_Open_Text_111455').style.display='none'; getElementById('Code_Closed_Image_111455').style.display='inline'; getElementById('Code_Closed_Text_111455').style.display='inline';
}
}
}
}
}" id="Code_Open_Image_111455">Code
public void Page_Load(object sender, System.EventArgs e)
{
// Change the current path so that the Routing handler can correctly interpret
// the request, then restore the original path so that the OutputCache module
// can correctly process the response (if caching is enabled).
string originalPath = Request.Path;
HttpContext.Current.RewritePath(Request.ApplicationPath, false);
IHttpHandler httpHandler = new MvcHttpHandler();
httpHandler.ProcessRequest(HttpContext.Current);
HttpContext.Current.RewritePath(originalPath, false);
}
仔細閱讀這段代碼你會發現,其實它的作用就是將地址轉換到根上,也就是將http//localhost:4804/default.aspx轉換成http//localhost:4804/,那麼路由就可以根據url來選擇相應的Controller和action了。