Adding a Controller
MVC stands for Model, View, Controller.
MVC is a pattern for developing applications such that each part has a responsibility that is different from another.
- Model: The data of 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 back to the client
We'll be covering all these concepts in this tutorial and show you how to use them to build an application.
Let's create a new controller by right-clicking the controllers folder in the solution Explorer and selecting Add Controller.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace Movies.Controllers
{
public class HelloWorldController : Controller
{
public string Index()
{
return "This is my default action...";
} public string Welcome()
{
return "This is the Welcome action method...";
}
}
}
Your Controller is named HelloWorldController and your new Method is called Index.
Run your application again, just as before (click the play button or press F5 to do this).
Once your browser has started up, change the path in the address bar tohttp://localhost:xx/HelloWorld where xx is whatever number your computer has chosen.
Now your browser should look like the screenshot below.
In our method above we returned a string passed into a method called "Content."
We told the system just returns 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 run:
/[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 visit /HelloWorld above and the method Index was implied.
This is because a method named “Index” is the default method that will be called on a controller if one is not explicitly specified.
Now, let's visit http://localhost:xx/HelloWorld/Welcome.
Now our Welcome Method has executed and returned its HTML string.
Again, /[Controller]/[ActionName]/[Parameters] so Controller is HelloWorld and Welcome is the Method in this case.
We haven't done Parameters yet.
Let's modify our sample slightly so that we can pass some information in from the URL to our controller, for example like this: /HelloWorld/Welcome?name=Scott&numtimes=4.
Change your Welcome method to include two parameters and update it like below.
Note that we've used the C# optional parameter feature to indicate that the parameter numTimes should default to 1 if it's not passed in.
public string Welcome(string name, int numTimes = )
{
string message = "Hello " + name + ", NumTimes is: " + numTimes;
return "" + Server.HtmlEncode(message) + "";
}
Run your application and visit http://localhost:xx/HelloWorld/Welcome?name=Scott&numtimes=4 changing the value of name and numtimes as you like.
The system automatically mapped 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 all the work, and has been returning HTML directly.
Ordinarily we don't want our Controllers returning HTML directly - since that ends up being very cumbersome to code.
Instead we'll typically use a separate View template file to help generate the HTML response.
Let's look at how we can do this.
Close your browser and return to the IDE.
Adding a Controller的更多相关文章
- 006.Adding a controller to a ASP.NET Core MVC app with Visual Studio -- 【在asp.net core mvc 中添加一个控制器】
Adding a controller to a ASP.NET Core MVC app with Visual Studio 在asp.net core mvc 中添加一个控制器 2017-2-2 ...
- 2.Adding a Controller
MVC stands for model-view-controller. MVC is a pattern for developing applications that are well ar ...
- ASP.NET Core 中文文档 第二章 指南(4.2)添加 Controller
原文:Adding a controller 翻译:娄宇(Lyrics) 校对:刘怡(AlexLEWIS).何镇汐.夏申斌.孟帅洋(书缘) Model-View-Controller (MVC) 架构 ...
- @Controller和@RestController的区别
1. Controller, RestController的共同点 都是用来表示spring某个类的是否可以接收HTTP请求 2. Controller, RestController的不同点 @C ...
- Spring中@Controller和@RestController之间的区别
1. Controller, RestController的共同点 都是用来表示Spring某个类的是否可以接收HTTP请求 2. Controller, RestController的不同点 @C ...
- @Controller和@RestController之间的区别
1. Controller, RestController的共同点 都是用来表示Spring某个类的是否可以接收HTTP请求 2. Controller, RestController的不同点 @Co ...
- 【spring Boot】Spring中@Controller和@RestController之间的区别
spring Boot入手的第一天,看到例子中的@RestController ............. 相同点:都是用来表示Spring某个类的是否可以接收HTTP请求 不同点:@Controll ...
- Getting Started with ASP.NET Web API 2 (C#)
By Mike Wasson|last updated May 28, 2015 7556 of 8454 people found this helpful Print Download Com ...
- 【ASP.NET Identity系列教程(一)】ASP.NET Identity入门
注:本文是[ASP.NET Identity系列教程]的第一篇.本系列教程详细.完整.深入地介绍了微软的ASP.NET Identity技术,描述了如何运用ASP.NET Identity实现应用程序 ...
随机推荐
- CDN技术详解
CDN,全称为Content DeliveryNetwork,中文意为"内容分发网络"".通过将网络内容发布到最靠近用户的『边缘节点』,使不同地区的用户在访问相同页面.图 ...
- div中iframe高度自适应问题
网页分为上.中.下三部分,上.下高度固定中间高度自适应:中间分为左.右两部分,左边宽度固定,右边宽度自适应.现在右侧div是宽度和高度都是自适应,右侧div里有个IFrame,想让IFrame自适应外 ...
- 通过Chrome浏览器检测和优化页面
1.访问(http://www.cnblogs.com/viaiu/) 2.点击F12 前两步就在扯淡 3.点击Audits标签,进入测试界面 4.点击按钮开始检测 5.如下图可以进行页面加载资源的详 ...
- Bloomberg面经准备: Josephus problem
Given a circular single linked list.Write a program that deletes every kth node until only one node ...
- Opennms 问题整理
1.网页时间显示不正确,需要修改:bin/opennms: 添加:MANAGER_OPTIONS="$MANAGER_OPTIONS -Duser.timezone=Asia/Shangha ...
- java实现二分查找
/** * 二分查找 * @param a * @param n * @param value * @return * @date 2016-10-8 * @author shaobn */ publ ...
- WebIM 聊天 Demo
最近 2 个月用业余时间写了一个 IM ,动手之前想了很多,包括前期设计.语言.数据库等,经过了一番思想斗争,最终前台用 Vue.js 展示,Server 使用 node ,数据库使用 MongoDB ...
- 【转】安装mysql 出现:Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist
来源:http://blog.csdn.net/dapeng0112/article/details/37053407 本来初始化配置是这样的: scripts/mysql_install_db -- ...
- LA 4255 UVa1423 拓扑排序
题目给出的是Sij的正负号,Sij=ai+...+aj,所以令前缀和Bi=a0+a1+..+ai,a0=0,B0=0,则有Sij=Bj-B(i-1): 由此构造出Bi的拓扑序列,只要每个拓扑序列相邻的 ...
- 227 Entering Passive Mode (xxx,xxx,,xxx,xxx,x)
登录ftp时显示227 Entering Passive Mode (xxx,xxx,,xxx,xxx,x) 因为FTP有两种工作模式,PORT方式和PASV方式,中文意思为主动式和被动式 ,详细介绍 ...