一、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的方法名字一致,否則訪問不成功