ASP.NET MVC2 第二章Ⅰ

來源:互聯網
上載者:User
§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

                

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

 

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.

 

Figure 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

Figure 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:

相關文章

聯繫我們

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