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.
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.
分享到:
相关推荐
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...
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 ...
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 632 pages Publisher: Apress; 1 edition (February 20, 2013) Language: English ISBN-10: 1430247738 ISBN-13: 978-1430247739 What you’ll learn ...
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-...
// Adding it to the page; var pic = '/Content/' + msg.filename; //$("#photos").attr("src", pic); $("#photos").append(';" src="' + pic + '"/>'); } }); //错误 webcam.set_hook('onError', ...
- **Building Your First Web API with ASP.NET Core MVC and Visual Studio**:介绍如何使用 ASP.NET Core MVC 和 Visual Studio 构建第一个 Web API。 - **Deploy an ASP.NET Core web app to Azure using Visual ...
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 ...
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, ...
2. 模块化架构:ASP.NET Core 的架构是模块化的,允许开发者根据需要选择使用哪些模块。 3. 高性能:ASP.NET Core 采用了异步编程模型,能够提供高性能的 Web 应用程序。 4. 灵活的 Routing 机制:ASP.NET Core 提供...
在ASP.NET开发中,添加构建横幅是一种常见做法,它有助于开发者在页面顶部或底部显示当前应用程序的版本信息、构建日期等,以便于追踪和管理不同的应用版本。这个过程涉及了Web开发的基本概念,包括HTML、CSS、以及...
在ASP.NET web应用程序开发中,日历控件(Calendar)是一种常用组件,它允许用户以图形化的方式选择日期。在你的项目中,你希望在用户双击日历上的日期时触发一个日程添加功能,这涉及到对控件事件的处理以及前端与...
“Adding ASP.NET Code to a Page”则讲解了如何将服务器端代码嵌入到网页中,这通常是通过代码隐藏(Code-Behind)模式实现的,代码隐藏将页面设计与业务逻辑分离,提高了代码的可维护性和复用性。 “Handling ...
- **使用ASP.NET MVC框架进行安全开发(Secure Development with the ASP.NET MVC Framework)**:聚焦于MVC框架下的安全编程实践,包括模型绑定、视图渲染和控制器动作的安全性。 ### 结论 本书全面覆盖了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 ...
<br>What you will learn from this book <br>How to create a better user experience by adding more dynamic UIs Steps for accessing ASP.NET profile and authentication services Ways to ...
It adopts ASP.NET technology, Client/Server (C/S) architecture, and SQL database for development, ensuring the security and stability of data to realize basic operations such as adding, deleting, ...
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 ...
- **Using ASP.NET MVC**: 介绍了 ASP.NET MVC 的基本概念及其在混合应用中的作用。 - **Combining routing for ASP.NET MVC, the ASP.NET Web API, and Angular**: 探讨了如何统一管理不同框架之间的路由。 - **...