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

  • 相关阅读:
    How To Change Database Name
    Oracle备份与恢复案例
    Linux 下安装 Oracle9i
    在 Linux x86 上安装 Oracle 数据库 10g_1
    在 Linux x86 上安装 Oracle 数据库 10g_2
    Oracle9i数据库DataGuard实施及维护手册2
    在 Linux x86 上安装 Oracle 数据库 10g_3
    流程企业(钢铁企业)的制造执行系统
    理解和使用Oracle分析工具LogMiner
    Oracle9i数据库Data Guard实施及维护手册 1
  • 原文地址:https://www.cnblogs.com/TivonStone/p/1850557.html
Copyright © 2011-2022 走看看