当前位置:网站首页>.net MVC understanding
.net MVC understanding
2022-07-28 17:43:00 【Cang jiuxiao Hai】
.net MVC The understanding of the ![]() One 、ASP.Net Two kinds of development mode of the Internet1.1 ASP.Net WebForm Development model
(1) Processing flow In traditional WebForm In mode , We ask for an example http://www.aspnetmvc.com/blog/index.aspx Of URL, So our WebForm The program will go to the root directory of the website to find blog In the catalog index.aspx file , Then from index.aspx Page CodeBehind file (.CS file ) Do logical processing , It may also include fetching data from the database ( What happened BLL To DAL Let's not talk about it here ), And then by index.aspx Page to present to the user . in summary , stay WebForm In mode : One URL The request is between the server and the URL The physical file on the corresponding path (ASPX Documents or other ), Then the file will process the request and return the result to the client . (2) The development way
1.2 ASP.Net MVC Development model(1) Processing flow stay ASP.Net MVC in , What the client requested URL Is mapped to the corresponding Controller Go to , Then from Controller To handle the business logic , Maybe from Model Take data from , And then by Controller Choose the right one View Return to the client . Let's go back to the previous operation ASP.NET MVC Program access http://www.aspnetmvc.com/Home/Index This URL, It actually accesses HomeController Medium Index This Action. (2) Salient features Two 、MVC Two different interpretations of the modelMVC There are two ways to understand patterns : One is Performance mode , The other is Architecture mode . It divides the application into three main components, namely : View (View)、 controller (Controller) And models (Model). Now? , Let's see M-V-C What do they stand for ? M:Model It's mainly a component that stores or processes data ;Model In fact, it is to realize the operation of the business logic layer on the corresponding database of entity classes , Such as :CRUD. It includes data 、 Validation rule 、 Application information such as data access and business logic .( Add :ViewModel: Model view ) As an architectural pattern ,View Is responsible for displaying data , and Controller Is responsible for obtaining View Transmitted data , Then call the business logic layer to process the completed data and transfer it to View To display . and Model Then deal with business logic , And return the result to Controller. From the perspective of traditional three-tier architecture ,View and Controller All belong to UI layer , and Model Then across BLL And DAL layer . (2)MVC Understanding as a mode of expression (3)MVC Architecture pattern integrated model It can be seen that , about MVC The difference between the two different understandings of Model On the understanding of : take Model As a business model (BLL、DAL etc. ) Or as a view model (ViewModel). 3、 ... and 、WebForm vs MVC(1)WebForm advantage : 1. Provides a large number of server-side controls , It can be developed quickly ; 2.ViewState It is convenient to return data ; 3. The cost of learning is low ; shortcoming : 1. The encapsulation is too strong , Although the learning cost is low , Many low-level things are not very clear to beginners ; 2. The custom control is not flexible , It is not conducive to the cooperation between artists and developers , Often those server controls will cause errors if they are handled carelessly ; 3. ViewState The delivery in the page will cause a lot of traffic consumption ; TIP: of WebForm Server controls and ViewState Detailed introduction , Friends who don't know can read another blog post 《ASP.Net WebForm Learning notes :aspx Explore with server control 》. (2)MVC advantage : 1. It's easy to divide complex applications into Model(ViewModel)、View、Controller Three component model , The processing background logic code is well separated from the foreground display logic , It belongs to loose coupling relationship , In the application of large projects , It is easier for agile development and test driven development , It's very scalable ; 2. Because there are no server-side controls , So the programmer can control it more flexibly , The page is cleaner , No, ViewState; 3. By modifying the routing rules , You can control the generation of custom url, So control generation SEO Amicable URL It will be easier ; 4. Strong type View Realization 、Razor View 、Model Binding mechanism 、Model The verification mechanism of , Safer and more efficient ; shortcoming : High learning cost , Complicated structure , Unnecessary frequent access to unchanging data , It will also damage operational performance . Four 、 first ASP.Net MVC Program4.1 The file organization structure after the new project(1) Create a new one ASP.Net MVC 4 project , choice “ basic ” Configuration and “ASPX” View engine ( Not for the time being Razor engine ). (2)VS The organization structure of the basic file generated for us is shown in the following figure : It can be seen that ,VS By default, it helps us create Models、Views as well as Controllers Three folders of , These three folders constitute our ASP.Net MVC Model project . among ,Controllers Is the class file of all controllers , and Models Is the class file of all models , and Views It's all cshtml or aspx File of . 4.2 Controller “ Convention over configuration ” stay Controllers Create a new controller in , named HomeController. In the default Index This Action Create a new view in , The default name is Index that will do . (2) Every Controller All corresponding View A folder in , The name of the folder follows Controller Same name 4.3 Relevant conventions of views(1) All the views It must be put in Views Under the table of contents 4.4 A bridge for data transmission -ViewData And ViewBag First ,ViewData It's a Key/Value Right dictionary set data structure , Used in Controller and View Build a bridge to transfer data .
(2)ViewPage There's also one ViewData A property of
(3) Controller Action After method execution , return ViewResult, then MVC The framework is executing ExcuteResult When the method is used ,Controller Medium ViewData The data will be passed to ViewPage class , In fact, that is hold Controller Of ViewData Assign a value to ViewPage Page ViewData attribute .
PS:ViweBag In fact, it contains one layer Dynamic Of ViewData, What the two brothers share is a container .
(5)ViewData And ViewBag Comparison
(6) How to use... In programs ViewData And ViewBag ① stay Controller The code in
② stay View The code in
4.5 A preliminary understanding of the routing mechanismWe can know through debugging , stay MVC All requests in are attributed to the controller Action. therefore , All requests specify a specific Action,Url The format of is determined according to the routing rules . that , stay ASP.Net MVC What is the default routing rule of , Where was it set up ?
open App_Start Folder , Can find RouteConfig This class , see RouteConfig Method of this class , It turns out to be RegisterRoutes This method is for our ASP.Net MVC The project sets the default routing rules :{controller}/{action}/{id}, That is to say, we can pass http://localhost/Home/Index/1 such URL To access the project . If we want to change the default routing rules , For example, we want to use this URL:http://localhost/Home-Index-1 To access the project , Then directly change the above default routing rule to :{controller}-{action}-{id} that will do . ![]() |
边栏推荐
- 【C语言笔记分享】字符函数和字符串函数(建议收藏)
- 阿里P8架构师谈:成为架构师必须学好的七大知识点(含面试题)
- Master the key points of JVM interview
- 渗透测试大杀器kali安装配置
- 网上出现的js小数计算的除法优化
- Visual object class introduces Pascal VOC dataset
- An article takes you closer to the overview and principle of kubernetes
- Some things encountered in ionic
- Mqtt.fx connects to Alibaba cloud Internet of things platform
- 软件测试需求人才越来越多,走上测试道路的人却越来越少?
猜你喜欢

Solve package is not available (for R ve [package 'xxx' is not available (for R version x.y.z) "warning?]

Visual object class introduces Pascal VOC dataset

产品研发中第三方技术服务的管理

Jdwp unauthorized rapid utilization

Arya professional web automated test platform

【C语言进阶】——函数指针

es6 Promise

简单易用的APP专项测试工具iTest4.7.0发布啦

mmcv安装的办法

Students' 20 R language exercises
随机推荐
Blue Bridge Cup embedded competition resources and skills
Wild pointer recurrence
JS中为对象数组添加新对象结果导致数组中已存在的对象也修改了
Uparse rich text style of uni app
mmcv安装的办法
Talk about the measurement of "post release problems"
新人如何入门学习软件测试
[untitled]
Master JVM interview topics and answers offer get soft (with learning roadmap)
谈谈你知道的发布上线(二)
都说软件测试是IT行业最差的,是这样的吗?
Distinguish between the export of ES6 and the module.exports of nodejs
2021 年全国大学生数据统计与分析竞赛
将input type='file' 类型的图片文件转成base64
ng-repeat在迭代最后一个元素时执行一个方法
Map R language
R language sub() usage
Adding new objects to the object array in JS results in the modification of existing objects in the array
R语言 sub()用法
Random talk on test platform - platform construction ideas (Part 1)





