Asp.Net4.5 mvc4(二) 頁面建立與講解

來源:互聯網
上載者:User

標籤: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(二) 頁面建立與講解

聯繫我們

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