C#高级编程笔记 2016年10月26日 MVC入门 Controller
1、MVC的定义:
- Models: Classes that represent the data of the application and that use validation logic to enforce business rules for that data.
- Views: Template files that your application uses to dynamically generate HTML responses.
- Controllers: Classes that handle incoming browser requests, retrieve model data, and then specify view templates that return a response to the browser.
using System.Web.Mvc;
using System.Web.Routing; namespace MvcWebApplication
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }//当url中无输入时,默认到Home 控制器中的Index Action 里面
);
}
}
}
/ [Controller] / [ActionName] / [Parameters]
HelloWorldController
class. 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.Parameters
) is for route data. We'll see route data later on in this tutorial.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. You haven't used the [Parameters]
part of the URL yet.public static void RegisterRoutes(RouteCollection routes){
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);}
4、/ [Controller] / [ActionName] / [Parameters] 的理解
Welcome
method to include two parameters as shown below. Note that the code return HttpUtility.HtmlEncode("Hello " + name + ", NumTimes is: " + numTimes);}
name
and numtimes
in the URL. The ASP.NET MVC model binding system automatically maps the named parameters from the query string in the address bar to parameters in your method.
public string Welcome(string name, int ID = ){
return HttpUtility.HtmlEncode("Hello " + name + ", ID: " + ID);}
.png)

public static void RegisterRoutes(RouteCollection routes){
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);}
ID.
The Welcome
action method contains a parameter (ID
) that matched the URL specification in the RegisterRoutes
method.name
and numtimes
in parameters as route data in the URL. In the App_Start\RouteConfig.cs file, add the "Hello" route:public class RouteConfig{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
); routes.MapRoute(
name: "Hello",
url: "{controller}/{action}/{name}/{id}"
);
}}
/localhost:XXX/HelloWorld/Welcome/Scott/3
..png)

C#高级编程笔记 2016年10月26日 MVC入门 Controller的更多相关文章
- C#高级编程笔记2016年10月12日 运算符重载
1.运算符重载:运算符重重载的关键是在对象上不能总是只调用方法或属性,有时还需要做一些其他工作,例如,对数值进行相加.相乘或逻辑操作等.例如,语句if(a==b).对于类,这个语句在默认状态下会比较引 ...
- C#高级编程笔记 2016年10月8日运算符和类型强制转换
1.checked和unchecked 运算符 C#提供了checked 和uncheckde 运算符.如果把一个代码块标记为checked, CLR就会执行溢出检查,如果发生溢出,就抛出overfl ...
- 2016年10月26日 星期三 --出埃及记 Exodus 19:10-11
2016年10月26日 星期三 --出埃及记 Exodus 19:10-11 And the LORD said to Moses, "Go to the people and consec ...
- 10月26日 奥威Power-BI基于微软示例库(MSOLAP)快速制作管理驾驶舱 腾讯课堂开课啦
本次课是基于olap数据源的案例实操课,以微软olap示例库Adventure Works为数据基础. AdventureWorks示例数据库为一家虚拟公司的数据,公司背景为大型跨国生产 ...
- 2016年10月31日 星期一 --出埃及记 Exodus 19:16
2016年10月31日 星期一 --出埃及记 Exodus 19:16 On the morning of the third day there was thunder and lightning, ...
- 2016年10月30日 星期日 --出埃及记 Exodus 19:15
2016年10月30日 星期日 --出埃及记 Exodus 19:15 Then he said to the people, "Prepare yourselves for the thi ...
- 2016年10月29日 星期六 --出埃及记 Exodus 19:14
2016年10月29日 星期六 --出埃及记 Exodus 19:14 After Moses had gone down the mountain to the people, he consecr ...
- 2016年10月28日 星期五 --出埃及记 Exodus 19:13
2016年10月28日 星期五 --出埃及记 Exodus 19:13 He shall surely be stoned or shot with arrows; not a hand is to ...
- 2016年10月27日 星期四 --出埃及记 Exodus 19:12
2016年10月27日 星期四 --出埃及记 Exodus 19:12 Put limits for the people around the mountain and tell them, `Be ...
随机推荐
- [LeetCode] Flatten 2D Vector 压平二维向量
Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...
- Euler-Maruyama discretization("欧拉-丸山"数值解法)
欧拉法的来源 在数学和计算机科学中,欧拉方法(Euler method)命名自它的发明者莱昂哈德·欧拉,是一种一阶数值方法,用以对给定初值的常微分方程(即初值问题)求解.它是一种解决常微分方程数值积分 ...
- 《如何正确学习JavaScript》读后小结
在segmentfault上读的一篇学习JavaScript路线的文章,做个小结. 一.简介.数据类型.表达式和操作符 (1)<JavaScript权威指南>前言1-2章&< ...
- JavaScript模板引擎artTemplate.js——引入子模板
之前的例子都是单一结构的对象,如果遇到复杂对象结构,我们可以通过引入子模板来实现html的渲染. 依旧以之前的数据作为例子: <div id="content">< ...
- jquery-leonaScroll-1.3-自定义竖向自适应滚动条插件
下载链接地址:https://share.weiyun.com/9ac3ca3fb29648bb1aad1b83a76b123c (密码:4y9t)[含mini版] 欢迎使用leonaScroll-1 ...
- mysql-netstat
在Linux服务器中想要查看连接到服务器的所有IP地址只需要输入命令netstat -an就可以看到全部的资料. 该命令的常见参数供您参考: -a (all)显示所有选项,默认不显示LISTEN相关: ...
- Java学习笔记(二)
再次更新,有问题大家一起讨论 Java中的class文件转换成.java文件 下载转换器Java Decompiler 使用此转换器打开.jar文件 就可以看到由.class转换来的.java文件 亲 ...
- Maven命令
1. mvn help:describe 你是否因为记不清某个插件有哪些goal而痛苦过,你是否因为想不起某个goal有哪些参数而苦恼,那就试试这个命令吧,它会告诉你一切的. 参数: 1. -Dplu ...
- 获取iis网站信息
1.获取网站的url地址 var ht = GetWebPathAndWebName(""); var rootUrl = ht["webaddr"].ToSt ...
- phpunit 测试框架安装
PHPUnit是一个轻量级的PHP测试框架.它是在PHP5下面对JUnit3系列版本的完整移植,是xUnit测试框架家族的一员(它们都基于模式先锋Kent Beck的设计).来自百度百科 一.下载wg ...