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实现应用程序 ...
随机推荐
- CXF WebService整合SpringMVC的maven项目
首先推荐博客:http://www.cnblogs.com/xdp-gacl/p/4259481.html http://blog.csdn.net/hu_shengyang/article/de ...
- 用python实现计算1-2*((60-30+(-40/5)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))类似的公式计算
作业需求: 开发一个简单的python计算器 1.实现加减乘除及拓号优先级解析 2.用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 ...
- smb服务器命令
win +r win7下运行 //linux主机地址 sudo smbpasswd -a 用户名 添加用户和密码
- JAVA静态代码审查之checkstyle
技术总监来巡查,刚巧前段时间遇到了一个问题还没解决,就拉着大牛开问.结果,问题是解决了,还附带了另一个问题,或是要求出来,没啥技术含量,但是很麻烦的一个东西:代码格式. 之前我写代码,因为屏幕比较小, ...
- Android开源图表之树状图和饼状图的官方示例的整理
最近由于工作需要,所以就在github上搜了下关于chart的三方框架 官方地址https://github.com/PhilJay/MPAndroidChart 由于工作需要我这里整理了一份Ecli ...
- 3-PHP全部编码UTF-8
0-html <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> ...
- NDK开发-零散知识点整理
JavaVM 标准Java平台下,每一个Process可以产生很多JavaVM对象,但在Android平台上,每一个Process只能产生一个Dalvik VM对象,也就是说在Android进程中是通 ...
- java第四次作业
(一)一个字节 import java.io.*; /** * 实现文件复制 * @author Administrator * */public class CopyFile { public st ...
- 【译】Import Changes from Direct3D 11 to Direct3D 12
译者:林公子 出处:木木的二进制人生 转载请注明作者和出处,谢谢! 这是微软公布的Direct3D 12文档的其中一篇,此翻译留作学习记录备忘,水平有限,错漏难免,还望海涵. 原文链接是https:/ ...
- ASP.net如何保证EF操作类线程内唯一
说到线程内唯一,肯定会想到单例模式,但是如果多用户访问网站就会出现问题.ASP.net中有两种方法可以保证EF操作类线程内唯一(目前只会这两种,以后有好的方法再添加): 1.httpcontext(实 ...