ASP.NET MVC 学习笔记1 Talk about controller & route
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的更多相关文章
- ASP.NET MVC学习笔记-----Filter2
ASP.NET MVC学习笔记-----Filter(2) 接上篇ASP.NET MVC学习笔记-----Filter(1) Action Filter Action Filter可以基于任何目的使用 ...
- ASP.NET MVC学习笔记-----Filter
ASP.NET MVC学习笔记-----Filter(1) Filter类型 接口 MVC的默认实现 Description Authorization IAuthorizationFilter Au ...
- ASP.NET MVC学习笔记-----Filter(2)
接上篇ASP.NET MVC学习笔记-----Filter(1) Action Filter Action Filter可以基于任何目的使用,它需要实现IActionFilter接口: public ...
- ASP.NET MVC 学习笔记-2.Razor语法 ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础 反射的具体应用 策略模式的具体应用 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用 C#读取XML文件的基类实现
ASP.NET MVC 学习笔记-2.Razor语法 1. 表达式 表达式必须跟在“@”符号之后, 2. 代码块 代码块必须位于“@{}”中,并且每行代码必须以“: ...
- 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 ...
- ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础
ASP.NET MVC在原来ASP.NET的基础上抛弃了基于页面的架构风格,使用了全新的MVC(模型-视图-控制器)架构的一种技术. 目前,它和ASP.NET都共存在.NET Framework之上. ...
- ASP.NET MVC 学习笔记(1)
从头开始系统地学习ASP.NET MVC 为什么要学习ASP.NET MVC?原因很多,可以先来看一下最早的ASP.NET WebForm的一些缺点: 传说中面试经常要问到的ASP.NET WebFo ...
- 【转】ASP.NET MVC学习笔记-Controller的ActionResult
1. 返回ViewResult public ActionResult Index() { ViewData["Message"] = "Welcome ...
- ASP.NET MVC学习笔记-----使用自定义的View Engine
我们都知道在ASP.NET MVC中自带了Razor View Engine,Razor十分的强大,可以满足我们绝大部分的需要.但是ASP.NET MVC的高度可扩展性,使我们可以使用自定义的View ...
随机推荐
- PatentTips - Method and system for browsing things of internet of things on ip using web platform
BACKGROUND The following disclosure relates to a method and system for enabling a user to browse phy ...
- [Java][Spring]Spring事务不起作用 问题汇总
[Java][Spring]Spring事务不起作用 问题汇总 http://blog.csdn.net/szwangdf/article/details/41516239
- [Java开发之路](15)注解
1. 简单介绍 注解(也被称为元数据),为我们在代码中加入信息提供了一种形式化的方法. 注解在一定程度上是把元数据与源码文件结合在一起,而不是保存在外部文档中这一大趋势之下所催生的. 它能够提供用来完 ...
- AngularJs压缩时须要注意的事项
因为AngularJS是通过控制器构造函数的參数名字来判断依赖服务名称的.所以假设你要压缩控制器的JS代码.它全部的參数也同一时候会被压缩,这时候依赖注入系统就不能正确的识别出服务了. 假如我们的Co ...
- [Django] Creating an app, models and database
To add a new app, first cd to the project. Then run: python manage.py startapp scrumboard After that ...
- 小强的HTML5移动开发之路(35)——jQuery中的过滤器详解
1.基本过滤选择器 :first :last :not(selector) :selector匹配的节点之外的节点 :even :偶数 :odd :奇数 :eq(index) :gt(index) : ...
- 课堂随笔02--c#中string作为引用类型的特殊性
using System; namespace Test { class Test1 { static void Main(string[] args) { string str1 = "1 ...
- 微信支付-公众号支付H5调用支付详解
微信公众号支付 最近项目需要微信支付,然后看了下微信公众号支付,,虽然不难,但是细节还是需要注意的,用了大半天时间写了个demo,并且完整的测试了一下支付流程,下面分享一下微信公众号支付的经验. 一. ...
- 推荐:一个写的相当好的介绍C++单元测试框架Google Test (gtest) 教程
原文来自:http://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.html 虽然有点晚了,还是一口气读完了全部文章.作者言简意赅和明快的风格 ...
- ubuntu 16.0.4 中docker 部署 sqlserver 2017(四)
1. 从 Docker Hub 中拉出 SQL Server 2017 Linux 容器映像 sudo docker pull mcr.microsoft.com/mssql/server:2017- ...