標籤:remove nuget rar mode mvc razor 控制 put 請求
1. 關鍵詞--路由
配置整個Web系統的路徑結構,一般在 Global.asax.cs 中執行 RouteConfig.RegisterRoutes。
public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } }
- url模式:ASP.NET 會對應地將名稱映射為 controller、action、id;
- 預設的根位置:對應 Home 的 controller,Index 的 action;
- 在這樣的配置下,可直接存取 root、root/Home、root/Home/Index 都會指向同一個位置;
2. 關鍵詞--Controller
namespace WebApplication.Controllers{ public class HomeController : Controller { public ActionResult Index() { ViewBag.Greeting = DateTime.Now.Hour < 12 ? "Good morning" : "Good afternoon"; return View(); } }}
- 由路由系統引導進入 HomeController 下的 Index;
- 執行對應的視圖檔案-Index.cshtml;
- 在Index方法內可以 ViewBag.Greeting 這樣的方式向視圖檔案傳遞值;
3. 關鍵詞--視圖檔案
@{ ViewBag.Title = "Home Page";}<div class="jumbotron"> <h1>ASP.NET</h1> <p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p> <p><a href="http://asp.net" class="btn btn-primary btn-large">Learn more »</a></p></div><div class="row"> <div> <p>@ViewBag.Greeting, guest!</p> @Html.ActionLink("RSVP Now", "RsvpForm"); </div> <div class="col-md-4"> <h2>Getting started</h2> <p> ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that enables a clean separation of concerns and gives you full control over markup for enjoyable, agile development. </p> <p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301865">Learn more »</a></p> </div> <div class="col-md-4"> <h2>Get more libraries</h2> <p>NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.</p> <p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301866">Learn more »</a></p> </div> <div class="col-md-4"> <h2>Web Hosting</h2> <p>You can easily find a web hosting company that offers the right mix of features and price for your applications.</p> <p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301867">Learn more »</a></p> </div></div>
- 可以使用Razor視圖引擎解釋 @“ ” 的內容;
- Controller 中傳遞進來的 ViewBag.Greeting 在 cshtml 中發揮了作用;
- @Html.ActionLink("RSVP Now", "RsvpForm"); 以 httpGet 的方式向本控制器(Home)下的 RsvpForm Action 進行請求;
4. HttpGet、HttpPost 屬性
public class HomeController : Controller { public ActionResult Index() { ViewBag.Greeting = DateTime.Now.Hour < 12 ? "Good morning" : "Good afternoon"; return View(); } [HttpGet] public ActionResult RsvpForm() { return View(); } [HttpPost] public ActionResult RsvpForm(GuestResponse response) { return View("Thanks", response); } public ActionResult About() { ViewBag.Message = "Your application description page."; return View(); } public ActionResult Contact() { ViewBag.Message = "Your contact page."; return View(); } }
- 剛剛從視圖檔案發出的 RsvpForm Action 請求由於是 Get 方式,因此將觸發帶有 HttpGet 屬性的 RsvpForm 方法;
- 同理將觸發其視圖檔案 RsvpForm.cshtml;
5. Html 輔助器方法與模型繫結
@model WebApplication2.Models.GuestResponse @{ ViewBag.Title = "RsvpForm";}<!DOCTYPE html><html><head> <title>RsvpForm</title></head><body> @using (Html.BeginForm()) { <p>Your name: @Html.TextBoxFor(x => x.Name)</p> <p>Your email: @Html.TextBoxFor(x => x.Email)</p> <p>Your phone: @Html.TextBoxFor(x => x.Phone)</p> <p> Will you attend? @Html.DropDownListFor(x => x.WillAttend, new []{ new SelectListItem(){ Text = "Yes, I‘ll be there", Value = bool.TrueString }, new SelectListItem(){ Text = "No, I can‘t come", Value = bool.FalseString } }, "Choose an option") </p> <input type="submit" value="Submit RSVP" /> }</body></html>
- model 關鍵字將視圖檔案與 Model 層 WebApplication2.Models 命名空間下的 GuestResponse 進行強綁定;
- 在 Razor 的 Html輔助器中 使用 lambda 運算式產生對象作為 Html 的 input元素(input元素的屬性來自於模型);
- 模型繫結:input 提交後將發起 同名的POST 方法,同時將 input 元素的值來填充模型;
6. 模型繫結後值的使用
@model WebApplication2.Models.GuestResponse @{ ViewBag.Title = "Thanks";}<!DOCTYPE html><html><head> <title>RsvpForm</title></head><body> <div> <h1>Thank you, @Model.Name</h1> @if (Model.WillAttend == true) { @:It is great that you‘re coming, The drinks are already in the fridge! } else { @:Sorry to hear that you can‘t make it, but thanks for letting us know. } </div></body></html>
- 依然使用 Razor 對C#模型進行解釋並使用:Model.Name;
APS.NET MVC入門-運行基礎樣本