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可以对控制器方法进行缓存。

Asp.net MVC十问十答[译]的更多相关文章

  1. [译]Asp.net MVC 之 Contorllers(一)

    Asp.net MVC contorllers 在Ajax全面开花的时代,ASP.NET Web Forms 开始慢慢变得落后.有人说,Ajax已经给了Asp.net致命一击.Ajax使越来越多的控制 ...

  2. [译]Asp.net MVC 之 Contorllers(二)

    URL路由模块 取代URL重写 路由请求 URL路由模块的内部结构 应用程序路由 URL模式和路由 定义应用程序路由 处理路由 路由处理程序 处理物理文件请求 防止路由定义的URL 属性路由 书接上回 ...

  3. 【译】ASP.NET MVC 5 教程 - 11:Details 和 Delete 方法详解

    原文:[译]ASP.NET MVC 5 教程 - 11:Details 和 Delete 方法详解 在教程的这一部分,我们将研究一下自动生成的 Details 和Delete 方法. Details ...

  4. 【译】ASP.NET MVC 5 教程 - 10:添加验证

    原文:[译]ASP.NET MVC 5 教程 - 10:添加验证 在本节中,我们将为Movie模型添加验证逻辑,并确认验证规则在用户试图使用程序创建和编辑电影时有效. DRY 原则 ASP.NET M ...

  5. 【译】ASP.NET MVC 5 教程 - 9:添加新字段

    原文:[译]ASP.NET MVC 5 教程 - 9:添加新字段 在本节中,我们将使用Entity Framework Code First 数据迁移功能将模型类的改变应用到数据库中. 默认情况下,当 ...

  6. 【译】ASP.NET MVC 5 教程 - 8:搜索查询

    原文:[译]ASP.NET MVC 5 教程 - 8:搜索查询 添加一个搜索的方法和搜索的视图 在本节中,我们为 Index 方法添加查询功能,使我们能够根据电影的题材或名称进行查找. 修改 Inde ...

  7. 【译】ASP.NET MVC 5 教程 - 7:Edit方法和Edit视图详解

    原文:[译]ASP.NET MVC 5 教程 - 7:Edit方法和Edit视图详解 在本节中,我们继续研究生成的Edit方法和视图.但在研究之前,我们先将 release date 弄得好看一点.打 ...

  8. 【译】ASP.NET MVC 5 教程 - 6:通过控制器访问模型的数据

    原文:[译]ASP.NET MVC 5 教程 - 6:通过控制器访问模型的数据 在本节中,你将新建一个MoviesController 类,并编写获取电影数据的代码,使用视图模板将数据展示在浏览器中. ...

  9. 【译】ASP.NET MVC 5 教程 - 5:使用 SQL 服务器 LocalDB 创建连接字符串

    原文:[译]ASP.NET MVC 5 教程 - 5:使用 SQL 服务器 LocalDB 创建连接字符串 在上一节中,我们创建了MovieDBContext 类来连接数据库.处理Movie 对象和数 ...

随机推荐

  1. 【转】IOS高级教程1:处理1000张图片的内存优化

    转载请保留以下原文链接: http://my.oschina.net/taptale/blog/91894 一.项目需求 在实际项目中,用户在上传图片时,有时会一次性上传大量的图片.在上传图片前,我们 ...

  2. 【转】IOS中的release和nil

    nil和release的作用: nil就是把一个对象的指针置为空,只是切断了指针与内存中对象的联系:而release才是真正通知内存释放这个对象. 所以nil并没有释放内存,只有release才回真正 ...

  3. iOS设计模式 - 命令模式

    前言: 命令对象封装了如何对目标执行指令的信息,因此客户端或调用者不必了解目标的任何细节,却仍可以对他执行任何已有的操作.通过把请求封装成对象,客 户端可 以把它参数化并置入队列或日志中,也能够支持可 ...

  4. Effective Java 78 Consider serialization proxies instead of serialized instances

    Serialization proxy pattern private static nested class (has a single constructor whose parameter ty ...

  5. 编译流程,C开发常见文件类型名

    编译流程 我们常说的编译是一个整体的概念,是指从源程序到可执行程序的整个过程,实际上,C语言编译的过程可以进一步细分为预编译->编译->汇编->链接 预编译是把include关键字所 ...

  6. Gulp使用入门操作十一步压缩JS

    前提需要安装nodejs 一. 全局安装Gulp npm install -g gulp 二.新建一个 gulpfile.js 文件 chapter2└── gulpfile.js 三.在 gulpf ...

  7. Jena学习笔记(2)——利用数据库保存本体

    注明:本文档是使用Jena2.6.4,数据库为MySQL,数据库驱动版本为mysql-connector-java-5.1.13-bin.jar. 1 Jena的数据库接口 Jena提供了将RDF数据 ...

  8. nyoj 42 一笔画问题 欧拉路径

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=42 欧拉回路,欧拉路径水题~ 代码: #include "stdio.h&quo ...

  9. KD Tree算法

    参考:http://blog.csdn.net/v_july_v/article/details/8203674 #!/user/bin/env python # -*- coding:utf8 -* ...

  10. Hive DDL DML SQL操作

    工作中经常要用到的一些东西,一直没整理,用的多的记住了,用的不多的每次都是去查,所以记录一下. DDL(数据定义语言),那就包括建表,修改表结构等等了 建表:create hive table hiv ...