zoukankan      html  css  js  c++  java
  • ASP.NET MVC2 第二章Ⅰ

    §2.1 Preparing Your Workstation

    To build an ASP.NET MVC 2 application, you need either of the following:

    • Visual Studio 2010 (any edition) or the free Visual Web Developer 2010 Express.These include ASP.NET MVC 2 by default.
    • Visual Studio 2008 with SP1 (any edition) or the free Visual Web Developer 2008 Express with SP1. These do not include ASP.NET MVC 2 by default; you must alsodownload and install ASP.NET MVC 2 from www.asp.net/mvc/.
    • If you don’t have any of these, then the easiest way to get started is to download and use Microsoft’.Web Platform Installer, which is available free of charge from www.microsoft.com/web/.

    §2.2 Creating a New ASP.NET MVC Project

    you’ll have a choice of two templates to start your new project:

    • The ASP.NET MVC 2 Web Application template creates a small example application that you can build on. This includes prebuilt user registration,
      authentication, navigation, and a relaxing, blue-themed CSS stylesheet.
    • The ASP.NET MVC 2 Empty Web Application template sets up only the minimal set of files and folders that are needed for almost every ASP.NET MVC 2 application.

    Let’s get started: open Visual Studio and go to File --> New –> Project

    §2.2.1 Adding the First Controller

    1                 2

    Next, when HomeController.cs appears, remove any code that it already contains, and replace the
    whole HomeController class with this:

        public class HomeController : Controller
        {
            public string Index()
            {
                return "Hello MVC";
            }
        }
     
     

    §2.2.2 How Does It Know to Invoke HomeController?

    Under the default routing configuration, you could request any of the following URLs and it would be handled by the Index action on HomeController:

    • /
    • /Home
    • /Home/Index

    So, when a browser requests http://yoursite/ or http://yoursite/Home, it gets back the output from HomeController’s Index method. Right now, the output is the string Hello, world!.

    §2.3 Rendering Web Pages

    The next step is to produce some HTML output.

    §2.3.1  Creating and Rendering a View

    Your existing controller, HomeController, currently sends a plain-text string to the browser. In real applications you’re more likely to generate an HTML document, and you do so by using a view.rewrite the method as follows:

        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                return View();
            }
        }

    if you run now you will see a error

    4

    Figure 2–5. Error message shown when ASP.NET MVC can’t find a view template

     

    Again, I’m showing you this error message so you can see exactly what the framework is trying to do and so you don’t think there’s some hidden magic happening. It tells you not just that it couldn’t find any suitable view to render. but also where it tried looking for one.When the framework wants to find the default view for an action called Index on a controller called HomeController, it will check the four locations listed in Figure 2–5.

    5Figure 2–6. Adding a view template for the Index action

    Uncheck “Select master page” (since we’re not using master pages in this example) and then clickAdd.  This will create a brand new view file for you at the correct default location for your action method:
    ~/Views/Home/Index.aspx.

     

    we write the view code like follows:

    <body>
        <div>
            Hello World ( from View) !
        </div>
    </body>

    Then Press F5 to launch the application again, and you should see your view template at work

    6Figure 2–7. Output from the view

     

    Besides ViewResult, there are other types of objects you can return from an action. For example, RedirectResult performs a redirection, and HttpUnauthorizedResult forces the visitor to log in. These things are called action results.

     

     

    §2.3.2  Adding Dynamic Output

    Of course, the whole point of a web application platform is the ability to construct and display dynamic output. In ASP.NET MVC, it’s the controller’s job to construct some data, and the view’s job to render it as HTML.

    Now, alter your HomeController’s Index() action method (again) to add a string into ViewData:

        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                int hour = 2;
                ViewData["greeting"]=(hour<12?"GoodMorning":"GoodNoon");
                return View();
            }
        }
    and update your Index.aspx view template as follows
     
    <body>
        <div>
            <%=ViewData["greeting"] %> World ( from View) !
        </div>
    </body>

    when you run the application you will see the follows:

    7

  • 相关阅读:
    在maven工程中使用groovy
    groovy学习1搭建环境
    Android 中运行时权限获取联系人信息 Demo
    Android 拍照或相册选择照片进行显示缩放位图 Demo
    Android 热点相关操作
    Android 内嵌 HTML5 并进行交互
    AJAX实现局部刷新
    C#主要用于查询sql的web项目:查询以及页面显示数据非常缓慢的改进方案
    配置和读取web.config数据库
    web项目在服务器IIS7上的部署:达到内部网可以通过输入网页直接访问的效果
  • 原文地址:https://www.cnblogs.com/TivonStone/p/1850557.html
Copyright © 2011-2022 走看看