`
suizhikuo
  • 浏览: 27143 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

ASP.NET MVC 3 (Adding a Controller) (2/9)

 
阅读更多

Adding a Controller

MVC stands for model-view-controller. MVC is a pattern for developing applications such that each part has a separate responsibility:

  • Model: The data for your application.
  • Views: The template files your application will use to dynamically generate HTML responses.
  • Controllers: Classes that handle incoming URL requests to the application, retrieve model data, and then specify view templates that render a response to the client.

We'll be covering all these concepts in this tutorial and show you how to use them to build an application.

Create a new controller by right-clicking the Controllers folder in Solution Explorer and then selecting Add Controller.

Name your new controller "HelloWorldController" and click Add.

HelloWorldControllerCode

Notice in Solution Explorer on the right that a new file has been created for you named HelloWorldController.cs and that the file is open in the IDE.

Inside the new public class HelloWorldController block, create two new methods that look like the following code. We'll return a string of HTML directly from the controller as an example.

using System.Web.Mvc;

namespace MvcMovie.Controllers
{
  public class HelloWorldController : Controller
  {
    public string Index()
    {
      return "This is my <b>default</b> action...";
    }

    public string Welcome()
    {
      return "This is the Welcome action method...";
    }    
  }
}

Your controller is named HelloWorldController and your new method is named Index. Run the application (press F5 or Ctrl+F5). Once your browser has started up, append "HelloWorld" to the path in the address bar. (On my computer, it's http://localhost:43246/HelloWorld) Your browser will look like the screenshot below. In the method above, the code returned a string directly. We told the system to just return some HTML, and it did!

ASP.NET MVC invokes different controller classes (and different action methods within them) depending on the incoming URL. The default mapping logic used by ASP.NET MVC uses a format like this to control what code is invoked:

/[Controller]/[ActionName]/[Parameters]

The first part of the URL determines the controller class to execute. So /HelloWorld maps to the HelloWorldController class. The second part of the URL determines the action method on the class to execute. So /HelloWorld/Index would cause the Index method of the HelloWorldController class to execute. Notice that we only had to browse to /HelloWorld and the Index method was used by default. This is because a method named Index is the default method that will be called on a controller if one is not explicitly specified.

Browse to http://localhost:xxxx/HelloWorld/Welcome. The Welcome method runs and returns the string "This is the Welcome action method...". The default MVC mapping is /[Controller]/[ActionName]/[Parameters]. For this URL, the controller is HelloWorld and Welcome is the action method. We haven't used the [Parameters] part of the URL yet.

Let's modify the example slightly so that we can pass some parameter information from the URL to the controller (for example, /HelloWorld/Welcome?name=Scott&numtimes=4). Change your Welcome method to include two parameters as shown below. Note that we've used the C# optional-parameter feature to indicate that the numTimes parameter should default to 1 if no value is passed for that parameter.

public string Welcome(string name, int numTimes = 1) 
{ 
 string message = "Hello " + name + ", NumTimes is: " + numTimes; 
 return "" + Server.HtmlEncode(message) + ""; 
}

Run your application and browse to http://localhost:xxxx/HelloWorld/Welcome?name=Scott&numtimes=4. You can try different values for name and numtimes. The system automatically maps the named parameters from your query string in the address bar to parameters in your method.

In both these examples the controller has been doing the VC portion of MVC — that is, the view and controller work. The controller is returning HTML directly. Ordinarily we don't want controllers returning HTML directly, since that becomes very cumbersome to code. Instead we'll typically use a separate view template file to help generate the HTML response. Let's look next at how we can do this.

分享到:
评论

相关推荐

    ASP.NET MVC MSDN 文档 CHM

    Walkthrough: Adding ASP.NET AJAX Scripting to an MVC Project Action Filtering in MVC Applications How to: Deploy an ASP.NET MVC Application How to: Add a Custom MVC Test Framework in Visual Studio ASP...

    Test-Drive ASP.NET MVC

    1 Getting Started with ASP.NET MVC 1.1 How ASP.NET MVC Works 1.2 Installing MVC 1.3 MVC in Five Minutes: Building Quote-O-Matic 2 Test-Driven Development 2.1 TDD Explained 2.2 Test-Driving ...

    asp.net mvc

    Note Because Visual Studio 2008 and Visual Studio 2010 Beta 2 share a component of ASP.NET MVC 2, installing the ASP.NET MVC 2 Release Candidate release on a computer where Visual Studio 2010 ...

    ASP.NET MVC 4 Recipes: A Problem-Solution Approach

    ASP.NET MVC 4 Recipes: A Problem-Solution Approach 632 pages Publisher: Apress; 1 edition (February 20, 2013) Language: English ISBN-10: 1430247738 ISBN-13: 978-1430247739 What you’ll learn ...

    用asp.net MVC 中的方法 添加客户端菜单

    Adding Custom Menu in Html Helper class using Extension Method in ASP.Net MVC

    ASP.NET MVC Framework Unleashed

    2 Building a Simple ASP.NET MVC Application..............................................23 3 Understanding Controllers and Actions......................................................47 4 ...

    ASP.NET.MVC.5.with.Bootstrap.and.Knockout.js.1491914394

    With this practical book, you’ll learn how by combining the ASP.NET MVC server-side language, the Bootstrap front-end framework, and Knockout.js—the JavaScript implementation of the Model-View-...

    ASP.NET MVC Flash 在线拍照

    // Adding it to the page; var pic = '/Content/' + msg.filename; //$("#photos").attr("src", pic); $("#photos").append(';" src="' + pic + '"/&gt;'); } }); //错误 webcam.set_hook('onError', ...

    Apress.Beginning.ASP.NET.MVC.4.2013

    How to get started with ASP.NET MVC. What tools and components you’ll need and how to download and set up the demo application on your system. Understand the details of the MVC pattern with deep-...

    ASP.NET Core 1.1 For Beginners: How to Build a MVC Website

    ASP.NET Core 1.1 For Beginners: How to Build a MVC Website by Jonas Fagerberg English | 19 May 2017 | ASIN: B071VX7KN4 | 411 Pages | PDF | 6.66 MB Want to learn how to build ASP.NET Core 1.1 MVC Web ...

    Professional ASP.NET 3.5 SP1 Edition: In C# and VB(part1)

    Methods for adding AJAX capabilities to your ASP.NET applications * The many benefits of the new data access additions * Ways to use and extend the Provider Model for accessing data stores, ...

    Beginning ASP.NET 1.1 with Visual C# .NET 2003.pdf

    As ASP.NET is not a programming language in its own right, but rather a technology (as we shall explain in the book), we will be teaching some basic programming principles in Chapters 2 to 7 in C#, ...

    Adding a Build Banner to ASP.NET Pages

    Adding a Build Banner to ASP.NET Pages。

    SignalR Programming in Microsoft ASP.NET(MS,2014)

    Get definitive guidance on SignalR, a new library for ASP.NET developers that simplifies the process of adding real-time web functionality to your applications. Real-time web functionality enables ...

    Professional.ASP.NET.2.0.AJAX

    &lt;br&gt;What you will learn from this book &lt;br&gt;How to create a better user experience by adding more dynamic UIs Steps for accessing ASP.NET profile and authentication services Ways to ...

    SignalR Programming in Microsoft ASP.NET

    Get definitive guidance on SignalR, a new library for ASP.NET developers that simplifies the process of adding real-time web functionality to your applications. Real-time web functionality enables ...

    Debugging ASP.NET

    Debugging ASP.NET Jonathan Goodyear Brian Peek Brad Fox Publisher: Financial Times Prentice Hall First Edition October 19, 2001 ISBN: 0-7357-1141-0, 376 pages New Riders - Debugging ASP.NET ...

    SignalR Programming in Microsoft ASP.NET(pdf英文原版)

    Get definitive guidance on SignalR, a new library for ASP.NET developers that simplifies the process of adding real-time web functionality to your applications. Real-time web functionality enables ...

Global site tag (gtag.js) - Google Analytics