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. [Compose] 14. Build curried functions

    We see what it means to curry a function, then walk through several examples of curried functions an ...

  2. [Ramda] Convert a QueryString to an Object using Function Composition in Ramda

    In this lesson we'll use a handful of Ramda's utility functions to take a queryString full of name/v ...

  3. js中如何删除某个元素下面的所有子元素?(两种方法)

    js中如何删除某个元素下面的所有子元素?(两种方法) 一.总结 方法一:通过元素的innerHTML属性 元素element.innerHTML=""; 方法二:通过元素的remo ...

  4. ios开发视频播放后台下载功能实现 :1,ios播放视频 ,包含基于AVPlayer播放器,2,实现下载,iOS后台下载(多任务同时下载,单任务下载,下载进度,下载百分比,文件大小,下载状态)(真机调试功能正常)

    ABBPlayerKit ios开发视频播放后台下载功能实现 : 代码下载地址:https://github.com/niexiaobo/ABBPlayerKit github资料学习和下载地址:ht ...

  5. 切换-5.7-传统复制切换成GTID复制

    1.基本环境:     Master Slave MySQL版本 MySQL-5.7.16-X86_64 MySQL-5.7.16-X86_64 IP 192.168.56.156 192.168.5 ...

  6. Erlang/OTP 中文手册

    http://erldoc.com/ Open Telecom Platform application array asn1rt base64 binary calendar code dbg di ...

  7. tombstone问题分析

    tombstone文件包含了发生问题的进程ID信息 I/DEBUG ( 241): pid: 244, tid: 244, name: mediaserver >>> /system ...

  8. iOS RunLoop了解和使用

    RunLoop介绍和使用 上次讲了runtime,这次是runloop,虽然两者都是run开头的名词术语,但是在OC中,这两个东西压根没啥联系.这篇文章主要讲讲runloop的一些概念和用法.其中包含 ...

  9. [Android]Fragment自定义动画、动画监听以及兼容性包使用

    Fragment是Android在API 11之后加入的一个组件,对提高Android开发中的布局合理性和布局效率都有很大作用,尤其是在Android平板等大屏幕设备的开发中,Fragment的引入能 ...

  10. poj1639 Picnic Planning,K度限制生成树

    题意: 矮人虽小却喜欢乘坐巨大的轿车,车大到能够装下不管多少矮人.某天,N(N≤20)个矮人打算到野外聚餐.为了集中到聚餐地点,矮人A 要么开车到矮人B 家中,留下自己的轿车在矮人B 家,然后乘坐B ...