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]”.

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

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

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

1         // GET: /HelloWorld/Welcome/ 
2 
3         public string Welcome(string id)
4         {
5             return HttpUtility.HtmlEncode(String.Format("This is the Welcome action method... Hello world, you id is {0}", id));
6         }

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

1         public ActionResult Contact()
2         {
3             ViewBag.Message = "Your contact page.";//ViewBag is a good thing
4 
5             return View();
6         }

  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?

1         // GET: /HelloWorld/Welcome/ 
2 
3         public string Welcome(string name, string id)
4         {
5             return HttpUtility.HtmlEncode(String.Format("This is the Welcome action method... Hello {0}, you id is {1}", name, id));
6         }

 

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

 1 public class RouteConfig
 2 {
 3    public static void RegisterRoutes(RouteCollection routes)
 4    {
 5       routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 6 
 7       routes.MapRoute(
 8           name: "Default",
 9           url: "{controller}/{action}/{id}",
10           defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
11       );
12 
13       routes.MapRoute(
14            name: "Hello",
15            url: "{controller}/{action}/{name}/{id}"
16        );
17    }
18 }

  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官方教程的实际操作,仅作记录用途,英文绝大部分纯粹为本人手写,渣英语,有错敬请指出。)

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。