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.
2、MVC的路由调用:(在 App_Start中的RouteConfig.cs 中定义了路由调用的基本格式)

     RouteConfig.cs
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 里面
);
}
}
}
     
     ASP.NET MVC invokes different controller classes (and different action methods within  them) depending on the incoming URL. The default URL routing logic used by ASP.NET  MVC uses a format like this to determine what code to invoke:
 
/ [Controller] / [ActionName] / [Parameters]
 
     When you run the application and don't supply any URL segments, it defaults to  the "Home" controller and the "Index" action method specified in the defaults  section of the code above. 
  [Controller]
     The first part of the URL determines the controller class to execute. So /HelloWorld maps  to the HelloWorldController class. 
 
     [ActionName]
  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.
     [Parameters]
  The third part of the URL segment ( Parameters) is for route data. We'll see route data later on in this  tutorial.
 
     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. 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 }
);}
3、HttpUtility:System.Web.HttpUtility
 

     提供用于在处理Web请求时编码和解码 URL 的方法,此类不能被继承。
 
     
4、/ [Controller] / [ActionName] / [Parameters] 的理解

   
      Let's modify the example slightly so that you 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 the code 
 
public string Welcome(string name, int numTimes = 1) {
    return HttpUtility.HtmlEncode("Hello " + name + ", NumTimes is: " + numTimes);}
     
     Run your application and browse to the example URL (http://localhost:xxxx/HelloWorld/Welcome?name=Scott&numtimes=4).  You can try different values for 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.
 
 
Replace the Welcome method with the following code:
 
public string Welcome(string name, int ID = ){
return HttpUtility.HtmlEncode("Hello " + name + ", ID: " + ID);}
 
Run the application and enter the following URL:  http://localhost:xxx/HelloWorld/Welcome/3?name=Rick
 


 
##为什么这时的“ 3 ” 能够被识别,而不用输入 "gender = 3" 呢?
     这是路由中的源码:
     
 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 }
);}
 
     This time the third URL segment  matched the route parameter ID. The Welcome action method contains a parameter  (ID) that matched the URL specification in the RegisterRoutes method.
     在 url 中有参数 id , 于是MVC 便会自动匹配。
 
      In ASP.NET MVC applications, it's more typical to pass in parameters as route  data (like we did with ID above) than passing them as query strings. You could also  add a route to pass both the 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}"
);
}}
 
      Run the application and browse to /localhost:XXX/HelloWorld/Welcome/Scott/3.
 
 

 
 
 
 
 
 
 
 
 
 

C#高级编程笔记 2016年10月26日 MVC入门 Controller的更多相关文章

  1. C#高级编程笔记2016年10月12日 运算符重载

    1.运算符重载:运算符重重载的关键是在对象上不能总是只调用方法或属性,有时还需要做一些其他工作,例如,对数值进行相加.相乘或逻辑操作等.例如,语句if(a==b).对于类,这个语句在默认状态下会比较引 ...

  2. C#高级编程笔记 2016年10月8日运算符和类型强制转换

    1.checked和unchecked 运算符 C#提供了checked 和uncheckde 运算符.如果把一个代码块标记为checked, CLR就会执行溢出检查,如果发生溢出,就抛出overfl ...

  3. 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 ...

  4. 10月26日 奥威Power-BI基于微软示例库(MSOLAP)快速制作管理驾驶舱 腾讯课堂开课啦

    本次课是基于olap数据源的案例实操课,以微软olap示例库Adventure Works为数据基础.        AdventureWorks示例数据库为一家虚拟公司的数据,公司背景为大型跨国生产 ...

  5. 2016年10月31日 星期一 --出埃及记 Exodus 19:16

    2016年10月31日 星期一 --出埃及记 Exodus 19:16 On the morning of the third day there was thunder and lightning, ...

  6. 2016年10月30日 星期日 --出埃及记 Exodus 19:15

    2016年10月30日 星期日 --出埃及记 Exodus 19:15 Then he said to the people, "Prepare yourselves for the thi ...

  7. 2016年10月29日 星期六 --出埃及记 Exodus 19:14

    2016年10月29日 星期六 --出埃及记 Exodus 19:14 After Moses had gone down the mountain to the people, he consecr ...

  8. 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 ...

  9. 2016年10月27日 星期四 --出埃及记 Exodus 19:12

    2016年10月27日 星期四 --出埃及记 Exodus 19:12 Put limits for the people around the mountain and tell them, `Be ...

随机推荐

  1. [LeetCode] Android Unlock Patterns 安卓解锁模式

    Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...

  2. CDQ分治

    要求可以计算前面的操作对后面询问的贡献 BZOJ1176 #include <cstdio> #include <algorithm> using namespace std; ...

  3. Tower是个不错的项目管理开放平台

    简单,易用,轻量级,挺多大项目都在用. 目前公司的项目也在使用,但是从高层到底下,随意惯了,最终没有用起来. 感觉适合年轻激情的创业公司团队来使用. 附上地址:https://tower.im/

  4. Todo list and 学习心得

    1. 理论实践要区分起来学习,结合起来运用. 2. 内事不决问百度外事不决问谷歌 3. 一个人走的快,一群人走得远或者更快 2016-09-01 23:27:58  九月目标:对程序从编译到执行的整个 ...

  5. 一次kibana服务失败的排查过程

    公司在kubernetes集群上稳定运行数月的kibana服务于昨天下午突然无法正常提供服务,访问kibana地址后提示如下信息: 排查过程: 看到提示后,第一反应肯定是检查elasticsearch ...

  6. Android测试基础题(三)

    今天接着给大家带来的是Android测试基础题(三).    需求:定义一个排序的方法,根据用户传入的double类型数组进行排序,并返回排序后的数组 俗话说的好:温故而知新,可以为师矣 packag ...

  7. NSRunLoop的进一步理解

    iPhone应用开发中关于NSRunLoop的概述是本文要介绍的内容,NSRunLoop是一种更加高明的消息处理模式,他就高明在对消息处理过程进行了更好的抽象和封装,这样才能是的你不用处理一些很琐碎很 ...

  8. 北京培训记day4

    智商题QAQ-- T1:求>=n的最小素数,n<=10^18 暴力枚举n-n+100,miller-rabin筛法 T2:给定一个01矩阵,每次选择一个1并将(x,y)到(1,1)颜色反转 ...

  9. 通过WebStorm上传代码至github

    首先需要注册github帐号,具体方法自行百度/谷歌. 打开WebStorm,我用的是2016.2.4版本(如果你有edu邮箱的话,可以免费使用一年,不只是Webstorm,JetBrains全家桶都 ...

  10. Unity 组件不常用知识备注

    Rigidbody(刚体) Interpolate:当物体进行不规则移动时,通过上一帧的行为来进行平滑移动 Extrapolate:通过推算下一帧的行为来进行平滑移动 PhysicMaterial(物 ...