MVC入門學習筆記(一)第一個MVC應用程式(基礎知識)

來源:互聯網
上載者:User

標籤:blog   http   os   io   使用   ar   檔案   資料   2014   


下載了並安裝Visual Studio Ultimate 2013 RC 和 MS SQL Server 2014。使用最新版本,可以瞭解和學習到最新技術。






去微軟官方網站,學習MVC相關的教程:
http://www.asp.net/mvc


去聽微軟官方網站推薦的MVC視頻,雖然是英語授課,看操作即可......
http://pluralsight.com/training/Player?author=scott-allen&name=mvc4-building-m1-intro&mode=live&clip=0&course=mvc4-building


購買一本MVC的書《ASP.NET MVC 4 Web編程》:



最後是建立自己第一個MVC應用程式,選擇Visual C#語言和.NET Framework4.5:

.項目已經建立完成,可以看到解決方案中已經為我們建立了一些檔案,以及一些引用

   

   其中,檔案夾Controllers是放置控制器的地方(可以相當於以前的.cs檔案匯總到這個檔案夾了,可以這樣理解,但事實不是這樣的)

   檔案夾Models是存放資料模型的地方

   檔案夾Views是存放顯示頁面的地方(也就是以.aspx尾碼的檔案)

 

 我們回過頭看檔案夾路徑,已經和我們以前學習ASP.NET中的路線大相徑庭,其實這裡的路徑是重寫過的,具體是在哪裡重寫的,我們來看 "Global.asax",找到“Global.asax.cs”:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcDemo
{
    // 注意: 有關啟用 IIS6 或 IIS7 傳統模式的說明,
    // 請訪問 http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // 路由名稱
                "{controller}/{action}/{id}", // 帶有參數的 URL
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 參數預設值
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);
        }
    }
}

 

    其中核心代碼是:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // 路由名稱
                "{controller}/{action}/{id}", // 帶有參數的 URL
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 參數預設值
            );

        }

 

這裡利用了這個靜態方法改寫了路徑,可以看到 routes.MapRoute(
                "Default", // 路由名稱
                "{controller}/{action}/{id}", // 帶有參數的 URL
,這句話就是將來重寫的URL的規格形式

 

解釋我們的URL :http://localhost:2906/Home/Index 中的Home控制器下的一個Index方法,而不是我們平常看到的檔案路徑。

 

好了,先寫到這裡,繼續努力

MVC入門學習筆記(一)第一個MVC應用程式(基礎知識)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.