码迷,mamicode.com
首页 > Web开发 > 详细

APS.NET MVC入门-运行基础示例

时间:2017-03-04 18:36:59      阅读:1386      评论:0      收藏:0      [点我收藏+]

标签: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 &raquo;</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 &raquo;</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 &raquo;</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 &raquo;</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入门-运行基础示例

标签:remove   nuget   rar   mode   mvc   razor   控制   put   请求   

原文地址:http://www.cnblogs.com/Daniel-Liang/p/6501958.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!