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实现应用程序 ...
随机推荐
- android开发中的问题总结(一)
1.设置sdk路径 window->preferences->android->SDK location 中进行修改,如果出现红叉,就说明不正确,选择的路径必须包含tools的那个目 ...
- cursor or set-based
标题可能和正文不太相符.我主要是记录工作中遇到使用游标的语句改成普通set-based operation,执行时间快了很多. 1.游标语句 declare @startDate dateTime d ...
- iOS获取当前AppStore版本号与更新
- (void)checkUpdateWithAppID:(NSString *)appID success:(void (^)(NSDictionary *resultDic , BOOL isNe ...
- java内省机制及PropertyUtils使用方法
背景 一般情况下,在Java中你可以通过get方法轻松获取beans中的属性值.但是,当你事先不知道beans的类型或者将要访问或修改的属性名时,该怎么办?Java语言中提供了一些像java.bean ...
- 关于ssh_copy_id脚本解析
[oldgirl@module ~]$ more /usr/bin/ssh-copy-id #!/bin/sh # Shell script to install your public key on ...
- C# 利用ICSharpCode.SharpZipLib实现在线加密压缩和解密解压缩
这里我们选用ICSharpCode.SharpZipLib这个类库来实现我们的需求. 下载地址:http://icsharpcode.github.io/SharpZipLib/ 1.单个或多个文件加 ...
- eclipse自动补全快捷键失效,sysout用不了!
好久没写Java代码了,使用新版Neon的Eclipse Java EE IDE开发时,自动补全各种失败,sysout也各种用不了, 开始还以为是电脑卡比呢,原来是版本的快捷键不同了,修改方法如下! ...
- C++之路进阶——HDU1880(魔咒词典)
---恢复内容开始--- New~ 欢迎参加2016多校联合训练的同学们~ 魔咒词典 Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 3 ...
- 飞机大战编写以及Java的面向对象总结
面向对象课程完结即可编写一个简单的飞机大战程序.我觉得我需要总结一下 飞机大战中类的设计: 父类:FlyingObject(抽象类) 接口:Award .Enemy 子类:Hero.Bullet.Ai ...
- 通什翡翠商城大站协议邮件群发系统日发20-30万封不打码不换ip不需发件箱100%进收件箱
用一种新的技术思维去群发邮件一种不用换IP,不需要任何发件箱的邮件群发方式一种不需要验证码,不需要**代码变量的邮件群发方式即使需要验证码也能全自动识别验证码的超级智能软件教你最核心的邮件群发思维和软 ...