For the sake of learning programming better, I'd like to increase the frequency of using English. So, this note will be written in English.

  All my study materials are from http://www.asp.net/mvc/overview/getting-started/introduction/getting-started. Also, it's in English. This guidance is aim at learning hands-on ability. I hope to get used to it. Let's rock now.

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

  ASP.NET MVC website is based on these 3 elements. That's all I got. Maybe I'll find something else later.

  First, let's analysis the struct of the project:

  I'm also not familiar with it. Talk about it later.^_^

  All the website pages are available by a RESTful request which is format as “Http://[SiteName]/[Controller]/[ActionName]/[Parameters]”.

     public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");//IgnoreRoute()是RouteCollection路由表类的扩展方法,用于忽略指定的路由请求。这句意思是忽略对扩展名为.axd文件的请求。(保护数据?) routes.MapRoute(
name: "Default",//路由名称
url: "{controller}/{action}/{id}",//url格式需要,指出的是,action中参数的名称要与{id}同名,即参数名称要叫做id
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }//默认格式
);
}
}

  The code has shown how your RESTful request accessed to it's target controller and get the view.

  eg:http://localhost:55040/helloworld/welcome/  

         // GET: /HelloWorld/Welcome/ 

         public string Welcome(string id)
{
return HttpUtility.HtmlEncode(String.Format("This is the Welcome action method... Hello world, you id is {0}", id));
}

  And result is:

Above has shown the relation between "V" and "C". Generally, the "view" folder would have several .cshtml fixed files which are of the same name of methods in controller. Shown as below:

When requests arrived, controller will do something first, then return the view: (Maybe we can build some js there? But for now, I'm not sure.)

         public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";//ViewBag is a good thing return View();
}

  Above, you may get confused by the "viewbag". Well, you may see this. On rendering a page, @viewbage.KeyToVavlue will be dynamically compiled to it's value.

  What if we'd like to send more than one param?

         // GET: /HelloWorld/Welcome/ 

         public string Welcome(string name, string id)
{
return HttpUtility.HtmlEncode(String.Format("This is the Welcome action method... Hello {0}, you id is {1}", name, id));
}

  Maybe we can define a new route? Or even use a model?(I'm guessing that, I'm also on learning.)

 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}"
);
}
}

  And below is the result:

  For many MVC applications, the default route works fine. You'll learn later in this tutorial to pass data using the model binder, and you won't have to  modify the default route for that.

  In these examples the controller has been doing the "VC" portion  of MVC — that is, the view and controller work. The controller is returning HTML  directly. Ordinarily you don't want controllers returning HTML directly, since  that becomes very cumbersome to code. Instead we'll typically use a separate  view template file to help generate the HTML response. Let's look next at how  we can do this.

(PS:本人新手,大大请轻喷,以上内容是本人按照ASP.NET官方教程的实际操作,仅作记录用途,英文绝大部分纯粹为本人手写,渣英语,有错敬请指出。)

ASP.NET MVC 学习笔记1 Talk about controller & route的更多相关文章

  1. ASP.NET MVC学习笔记-----Filter2

    ASP.NET MVC学习笔记-----Filter(2) 接上篇ASP.NET MVC学习笔记-----Filter(1) Action Filter Action Filter可以基于任何目的使用 ...

  2. ASP.NET MVC学习笔记-----Filter

    ASP.NET MVC学习笔记-----Filter(1) Filter类型 接口 MVC的默认实现 Description Authorization IAuthorizationFilter Au ...

  3. ASP.NET MVC学习笔记-----Filter(2)

    接上篇ASP.NET MVC学习笔记-----Filter(1) Action Filter Action Filter可以基于任何目的使用,它需要实现IActionFilter接口: public ...

  4. ASP.NET MVC 学习笔记-2.Razor语法 ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础 反射的具体应用 策略模式的具体应用 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用 C#读取XML文件的基类实现

    ASP.NET MVC 学习笔记-2.Razor语法   1.         表达式 表达式必须跟在“@”符号之后, 2.         代码块 代码块必须位于“@{}”中,并且每行代码必须以“: ...

  5. ASP.NET MVC 学习笔记-7.自定义配置信息 ASP.NET MVC 学习笔记-6.异步控制器 ASP.NET MVC 学习笔记-5.Controller与View的数据传递 ASP.NET MVC 学习笔记-4.ASP.NET MVC中Ajax的应用 ASP.NET MVC 学习笔记-3.面向对象设计原则

    ASP.NET MVC 学习笔记-7.自定义配置信息   ASP.NET程序中的web.config文件中,在appSettings这个配置节中能够保存一些配置,比如, 1 <appSettin ...

  6. ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础

    ASP.NET MVC在原来ASP.NET的基础上抛弃了基于页面的架构风格,使用了全新的MVC(模型-视图-控制器)架构的一种技术. 目前,它和ASP.NET都共存在.NET Framework之上. ...

  7. ASP.NET MVC 学习笔记(1)

    从头开始系统地学习ASP.NET MVC 为什么要学习ASP.NET MVC?原因很多,可以先来看一下最早的ASP.NET WebForm的一些缺点: 传说中面试经常要问到的ASP.NET WebFo ...

  8. 【转】ASP.NET MVC学习笔记-Controller的ActionResult

    1. 返回ViewResult public ActionResult Index()   {       ViewData["Message"] = "Welcome ...

  9. ASP.NET MVC学习笔记-----使用自定义的View Engine

    我们都知道在ASP.NET MVC中自带了Razor View Engine,Razor十分的强大,可以满足我们绝大部分的需要.但是ASP.NET MVC的高度可扩展性,使我们可以使用自定义的View ...

随机推荐

  1. Uncaught SyntaxError: Invalid regular expression flags(看页面源代码)

    Uncaught SyntaxError: Invalid regular expression flags(看页面源代码) 一.总结 js或者jquery方面的错误看页面源代码,一下子错误就很清晰了 ...

  2. System.Xml.XmlException: 引用了未声明的实体“nbsp”

    在XML文件中<, >,&等有特殊含义,(前两个字符用于链接签,&用于转义),不能直接使用.使用这些个字符时,应使用它们的转义序列,下面是5个在XML文件中预定义好的实体: ...

  3. AngularJS之ng-class指令

    ng-class是AngularJS预设的一个指令,用于动态自定义dom元素的css class name. 在angular中为我们提供了3种方案处理class: 1:scope变量绑定. < ...

  4. ajax跳转到新的jsp页面(局部刷新)

    ajax可以实现局部刷新页面,即在不刷新整个页面的情况下更新页面的局部信息. 项目中遇到一个问题:在用户列表也,当点击某个按钮时需要去查询用户的信息,查询成功跳转到用户详情界面:查询失败,则在原页面弹 ...

  5. 忙里偷闲( ˇˍˇ )闲里偷学【C语言篇】——(3)输入输出函数

    一.基本的输入和输出函数的用法 1.printf()  //屏幕输出 用法: (1)printf("字符串\n"); (2)printf("输出控制符", 输出 ...

  6. Git提交到多个远程仓库(多看两个文档)

    Git提交到多个远程仓库(多看两个文档) 一.总结 一句话总结: 二. Git提交到多个远程仓库(多看两个文档) 有两种做法,先看第一种 一.通过命令行进行操作 例如我有下面两个仓库: Mybatis ...

  7. LaunchImage启动黑屏-模拟器可以,但是真机黑屏

    名称和尺寸都是对的,就是不显示.结果美工重新做一张图片就行了,想了半天都没想到是图片本身等问题啊

  8. MFC下WM_NOTIFY消息处理流程

    参考文章:MFC的消息反射机制 在前一篇文章:MFC消息处理流程概述中描述了MFC消息处理的大体流程.由CWnd::OnWndMsg函数可知,当消息为WM_NOTIFY消息时,调用的是virtual ...

  9. 【71.76%】【codeforces 732A】Buy a Shovel

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  10. java做微信支付notify_url异步通知服务端的写法

    最近团队在接入微信支付,APP和JSAPI的接口都需要填写一个notify_url回调地址,但是坑爹的官方文档并没有找到JSAPI模式的java版的demo,所以不得不自己看文档写了一个接受微信异步通 ...