標籤: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;" />
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;" />
<span style="font-size:14px;">routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}", //controller目錄下的,action匹配Controllers目錄下actionresult,id是一個可寫參數
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } //預設啟動項HomeController下的Index
);</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;" />
public ActionResult Next()
{
return View();
}
這個返回一個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;" />
public ActionResult Index()
{
ViewBag.joy = "姓名:";
ViewBag.name = "丁小未";
ViewData["data"] = "大家來學學ASP.NET MVC4吧!";
return View();
}
前台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;" />
@{
ViewBag.Title = "Index";
}
<h2>我的資訊</h2>
<a href="http://blog.csdn.net/dingxiaowei2013">學無止境的專欄</a>
<br/>
@ViewBag.joy
<br/>
@ViewBag.name
<br/>
@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