[MVC4]初識.NET MVC4

來源:互聯網
上載者:User

標籤:asp.net   mvc   

最近一個月都在專心做unity3d的鬥地主遊戲,從早到晚,最後總算是搞出來了,其中的心酸只有自己知道。最近才有功夫閑下來,還是學習學習之前的老本行——asp.net,現在用.net做項目流行MVC,而不是之前的三層,既然技術在更新,只能不斷學習,以適應新的技術潮流!

建立MVC工程1.開啟Visual studio2012,建立MVC4工程

650) this.width=650;" src="http://img.blog.csdn.net/20140608161353546" style="border:none;" />

2.選擇工程屬性,建立MVC工程

650) this.width=650;" src="http://img.blog.csdn.net/20140608161456500" style="border:none;" />

3.產生工程的目錄

650) this.width=650;" src="http://img.blog.csdn.net/20140608161625750" style="border:none;" />

  • App_Start:開機檔案的配置資訊,包括很重要的RouteConfig路由註冊資訊

  • Content檔案放置樣式檔案

  • Controllers檔案夾用來放置控制器

  • Scripts檔案夾系統自動建立了jquery檔案

  • Views檔案夾放置控制器的視圖檔案

4.RouteConfig檔案

[csharp] view plaincopyprint?650) this.width=650;" src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代碼片" style="border:none;" />650) this.width=650;" src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到My Code片" style="border:none;" />

  1. <span style="font-size:14px;">routes.MapRoute(  

  2.                 name: "Default",  

  3.                 url: "{controller}/{action}/{id}", //controller目錄下的,action匹配Controllers目錄下actionresult,id是一個可寫參數  

  4.                 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } //預設啟動項HomeController下的Index  

  5.             );</span>  

這個檔案是路由註冊檔案,配置預設啟動項,這裡指定的controller是HomeController,actionResult是Index,id可寫可不寫。

5.建立控制器

在Controllers檔案夾右擊->添加->控制器,保留Controller,取名HomeController

650) this.width=650;" src="http://img.blog.csdn.net/20140608163334031" style="border:none;" />

650) this.width=650;" src="http://img.blog.csdn.net/20140608163451437" style="border:none;" />

點擊添加,則建立成功。

查看產生的HomeController控制器

[csharp] view plaincopyprint?650) this.width=650;" src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代碼片" style="border:none;" />650) this.width=650;" src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到My Code片" style="border:none;" />

  1. public ActionResult Next()  

  2. {  

  3.      return View();  

  4. }  


這個返回一個ActionResult到前台視圖頁面,下面我們可以在此基礎上建立控制器視圖。

6.建立控制器視圖

在上面的ActionResult裡面右擊滑鼠,添加視圖,名字要跟方法名一致。

然後在Views檔案夾下自動產生一個該控制器的視圖

650) this.width=650;" src="http://img.blog.csdn.net/20140608164250640" style="border:none;" />

微軟做的太貼心了,哇哢哢!

7.在控制器中添加代碼,在視圖中顯示出來

我們在控制器Index方法中添加如下代碼,傳遞到view中

[csharp] view plaincopyprint?650) this.width=650;" src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代碼片" style="border:none;" />650) this.width=650;" src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到My Code片" style="border:none;" />

  1. public ActionResult Index()  

  2.         {  

  3.             ViewBag.joy = "姓名:";  

  4.             ViewBag.name = "丁小未";  

  5.             ViewData["data"] = "大家來學學ASP.NET MVC4吧!";  

  6.             return View();  

  7.         }  


前台view顯示方法

[csharp] view plaincopyprint?650) this.width=650;" src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代碼片" style="border:none;" />650) this.width=650;" src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到My Code片" style="border:none;" />

  1. @{  

  2.     ViewBag.Title = "Index";  

  3. }  

  4.   

  5. <h2>我的資訊</h2>  

  6. <a href="http://blog.csdn.net/dingxiaowei2013">學無止境的專欄</a>  

  7. <br/>  

  8. @ViewBag.joy  

  9. <br/>  

  10. @ViewBag.name  

  11. <br/>  

  12. @ViewData["data"]  


8.查看運行效果

由於預設的路由註冊,所以這兩個url效果是一樣的。

650) this.width=650;" src="http://img.blog.csdn.net/20140608153218625" style="border:none;" />        650) this.width=650;" src="http://img.blog.csdn.net/20140608153226000" style="border:none;" />

如果我們運行其他的view也是可以的,訪問HomeController下的NextAction視圖

650) this.width=650;" src="http://img.blog.csdn.net/20140608164822765" style="border:none;" />

也可以訪問其他控制器的Action視圖

650) this.width=650;" src="http://img.blog.csdn.net/20140608165113828" style="border:none;" />


歡迎關注我的圍脖


==================== 迂者 丁小未 CSDN部落格專欄=================

  MyQQ:1213250243      我的圍脖

Unity QQ群:375151422,858550,6348968         cocos2dx QQ群:280818155

====================== 相互學習,共同進步 ===================



本文出自 “丁小未的專欄” 部落格,請務必保留此出處http://dingxiaowei.blog.51cto.com/4561335/1423689

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.