1. Explain MVC (Model-View-Controller) in general?

MVC (Model-View-Controller) is an architectural software pattern that basically decouples various components of a web application. By using MVC pattern, we can develop applications that are more flexible to changes without affecting the other components of our application.

MVC是实现各种不同物体之间实现解耦的一种软件架构模式。利用MVC模式,我们可以开发出易于维护,同时不会影响到其他物体的应用来。

• "Model", is basically domain data.
Model,实际上代表 Domain 数据
• "View", is user interface to render domain data.
View,用于展示Domain数据的用户接口。
• "Controller", translates user actions into appropriate operations performed on model.
Controller,解释用户行为操控并提取数据进行相应。


2. What is ASP.NET MVC?

ASP.NET MVC is a web development framework from Microsoft that is based on MVC (Model-View-Controller) architectural design pattern. Microsoft has streamlined the development of MVC based applications using ASP.NET MVC framework.
Asp.net MVC是微软推出的一种基于MVC软件构架模式的网站开发框架。微软已经利用Asp.net mvc框架改进了传统的mvc的开发方式。

 

3. Difference between ASP.NET MVC and ASP.NET WebForms?

ASP.NET Web Forms uses Page controller pattern approach for rendering layout, whereas ASP.NET MVC uses Front controller approach. In case of Page controller approach, every page has its own controller i.e. code-behind file that processes the request. On the other hand, in ASP.NET MVC, a common controller for all pages processes the requests.
Asp.net Web Forms 利用Page Controller模式来进行展现,但是Asp.net mvc利用front Controller方式。对于Page Controller方式,每一个请求页面都会创建自己的Controller,他们会利用代码后置文件来处理请求。但是在Asp.net MVC中,一个普通的controller可以处理所有页面的所有请求。


 
4. What are the Core features of ASP.NET MVC?
 
Core features of ASP.NET MVC framework are:
•Clear separation of application concerns (Presentation and Business Logic)
•An extensible and pluggable framework
•Extensive support for ASP.NET Routing
•Support for existing ASP.NET features
Follow for detailed understanding of above mentioned core features.

Asp.net MVC框架的核心特色:
UI表示层和业务逻辑层隔离清晰,职责分明。
易于扩展和插件式开发。
完全支持Asp.net 路由。
支持已有的Asp.net 特性。

 
5. Can you please explain the request flow in ASP.NET MVC framework?
 
Request flow for ASP.NET MVC framework is as follows:
Request hits the controller coming from client. Controller plays its role and decides which model to use in order to serve the request. Further passing that model to view which then transforms the model and generate an appropriate response that is rendered to client.
 用户请求首先会达到Controller,Controller将会决定使用哪个model来响应请求。为了将请求到的model发送出去,Controller会将请求到的model进行转换,然后生成准确的响应,最终传送给客户端。


6. What is Routing in ASP.NET MVC?


In case of a typical ASP.NET application, incoming requests are mapped to physical files such as .aspx file. ASP.NET MVC framework uses friendly URLs that more easily describe user’s action but not mapped to physical files.
对于传统的Asp.net应用来说,路由是 用户请求对物理文件的映射,比如对.aspx文件。在Asp.net MVC框架中,路由则使用了友好的URL的方式来描述用户行为,但是不会对物理文件产生映射。

ASP.NET MVC framework uses a routing engine, that maps URLs to controller classes. We can define routing rules for the engine, so that it can map incoming request URLs to appropriate controller.
Asp.net MVC框架使用了路由引擎,用来产生对Controller类的URL映射。我们可以指定路由访问规则,以便于能够将收到的URL请求映射到准确的Controller中。

Practically, when a user types a URL in a browser window for an ASP.NET MVC application and presses “go” button, routing engine uses routing rules that are defined in Global.asax file in order to parse the URL and find out the path of corresponding controller.
事实上,当用户在浏览器输入URL,然后点击“Go”按钮的时候,路由引擎使用配置在Global.asax文件中的路由规则去解析URL,以便于找到正确的Controller的访问路径。


 
7. What is the difference between ViewData, ViewBag and TempData?
 
In order to pass data from controller to view and in next subsequent request, ASP.NET MVC framework provides different options i.e. ViewData, ViewBag and TempData.

Both ViewBag and ViewData are used to to communicate between controller and corresponding view.But this communication is only for server call, it becomes null if redirect occurs. So, in short, its a mechanism to maintain state between controller and corresponding view.
 
ViewData is a dictionary object while ViewBag is a dynamic property (a new C# 4.0 feature). viewData being a dictionary object is accessible using strings as keys and also requires typecasting for complex types.On the other hand, ViewBag doesn't have typecasting and null checks.
 
TempData is also a dictionary object that stays for the time of an HTTP Request. So, Tempdata can be used to maintain data between redirects i.e from one controller to the other controller.

为了处理后续的controller到View的数据传送请求,asp.net mvc框架提供了不同的操作方式: ViewData,ViewBag以及TempData。

ViewBag和ViewData用于Controller和View之间的一致性传递。但是这种传递只在服务端使用,如果使用了Redirect方法,他们将会出现NULL值,所以,简单的说来,他们是用于维护controller和view之间状态一致性的一种机制。

ViewData是一种字典对象,但是ViewBag却拥有dynamic属性(C# 4.0的新特性)。 ViewData由于是字典对象,所以可以使用字符串作为键进行访问,但是当使用复杂类型作为键的时候,则需要进行类型转换。但是对于ViewBag来说,则无需进行类型转换和NULL值监测。
 
8. What are Action Methods in ASP.NET MVC?
 
As I already explained about request flow in ASP.NET MVC framework that request coming from client hits controller first. Actually MVC application determines the corresponding controller by using routing rules defined in Global.asax. And controllers have specific methods for each user actions. Each request coming to controller is for a specific Action Method. The following code example, “ShowBooks” is an example of an Action Method.
 
public ViewResult ShowBooks(int id)
{
  var computerBook = db.Books.Where(p => P.BookID == id).First();
  return View(computerBook);
}
 由于我已经讲解了在Asp.net mvc 框架中的客户端请求到控制器的请求流程。事实上MVC应用会使用Global.asax中的路由规则来请求对应的控制器。 每一个用户操作都会有控制器中的方法与之对应。每一个到达控制器的请求都会有一个Action方法与之对应。下面的ShowBooks代码示例就是一个Action方法请求的例子:
public ViewResult ShowBooks(int id)
{
  var computerBook = db.Books.Where(p => P.BookID == id).First();
  return View(computerBook);
}
 
9.Explain the role of Model in ASP.NET MVC?
 
One of the core feature of ASP.NET MVC is that it separates the input and UI logic from business logic. Role of Model in ASP.NET MVC is to contain all application logic including validation, business and data access logic except view i.e. input and controller i.e UI logic.
 
Model is normally responsible for accessing data from some persistent medium like database and manipulate it.
 Asp.net MVC的一个核心特性就是它解耦了业务处理和UI逻辑。在Asp.net MVC中,Model的角色就是用来处理所有的应用逻辑的,包括验证,业务逻辑,数据访问逻辑,但是数据展现则除外。也就是说input和controller包含其中,但是UIlogic排除在外。
 
10.What are Action Filters in ASP.NET MVC?
 
If we need to apply some specific logic before or after action methods, we use action filters. We can apply these action filters to a controller or a specific controller action. Action filters are basically custom classes that provide a mean for adding pre-action or post-action behavior to controller actions.
For example,
•Authorize filter can be used to restrict access to a specific user or a role.
•OutputCache filter can cache the output of a controller action for a specific duration.

如果我们需要在Action方法之前或者之后应用一些特殊的逻辑, 我们可以使用Action Filters。我们可以将这些Action Filters应用在Controller或者是controller中的action上面。事实上,Action Filters只是一个提供了添加方法执行前行为和方法执行后行为的规则的普通类。
比如说:
Authorize Filter可以对特定的用户或者角色进行访问限制验证。
OutputCache Filter可以对控制器方法进行缓存。

posted on 2013-11-28 10:44  程序诗人  阅读(384)  评论(0编辑  收藏  举报