標籤:style blog http color io os strong 檔案 sp
一、Contorl
通過目錄結構我們可以看到contorllers類的命名方式
命名規則:首碼+Controller.
在看看contorller中的action方法
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace shuangFirstMvc.Controllers{ public class HomeController : Controller { public ActionResult Index() { ViewBag.Message = "修改此模板以快速啟動你的 ASP.NET MVC 應用程式。"; return View(); } public ActionResult About() { ViewBag.Message = "你的應用程式說明頁。"; return View(); } public ActionResult Contact() { ViewBag.Message = "你的連絡方式頁。"; return View(); } }}
每個方法都會通過“return view()”返回一個視圖,我們理解為每一個方法名就代表一個視圖頁面
所以前台訪問的路徑為:http://網域名稱/contorller首碼/action方法名,如http://http://localhost:40893/Account/Login
二、建立一個頁面看看效果
效果:開啟http://localhost:40893/Demo/demo,顯示:這是我的第一個頁面
首先在controllers中建立contorller,命名為DemoController
右擊添加-控制器
然後在代碼上寫上
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace shuangFirstMvc.Controllers{ public class DemoController : Controller { // // GET: /Demo/ public ActionResult Index() { return View(); } public ActionResult demo() { return Content("這是我的第一個頁面"); } }}
如果頁面需要修飾,我們可以建立一個視圖
建立檔案夾Demo,然後在Demo下面建立demo.cshtml視圖
@{ Layout = null;}<!DOCTYPE html><html><head> <meta name="viewport" content="width=device-width" /> <title>demo</title></head><body> <div> 這是我的第一個頁面 </div></body></html>
建立視圖的時候首先建立檔案夾,名字和contorller首碼一致,具體的頁面名稱要和action的方法名字一致,否則訪問不成功
Asp.Net4.5 mvc4(二) 頁面建立與講解