asp.net-mvc ASP.NET MVC 3 和 4 之间的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12535327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between ASP.NET MVC 3 and 4?
提问by Akshat Jiwan Sharma
Is there a comprehensive list that explains all of the new features of MVC4 and what all has changed from MVC3?
是否有一个完整的列表来解释 MVC4 的所有新特性以及与 MVC3 相比发生了什么变化?
(The release notesare not much helpful)
(发行说明没有多大帮助)
采纳答案by JSK NS
Copied and pasted from MVC4 Release Notes:
从MVC4 发行说明复制并粘贴:
?Modern HTTP programming model: Directly access and manipulate HTTP requests and responses in your Web APIs using a new, strongly typed HTTP object model. The same programming model and HTTP pipeline is symmetrically available on the client through the new HttpClient type.
? 现代 HTTP 编程模型:使用新的强类型 HTTP 对象模型直接访问和操作 Web API 中的 HTTP 请求和响应。相同的编程模型和 HTTP 管道通过新的 HttpClient 类型在客户端对称可用。
?Full support for routes: ASP.NET Web API supports the full set of route capabilities of ASP.NET Routing, including route parameters and constraints. Additionally, use simple conventions to map actions to HTTP methods.
? 完全支持路由:ASP.NET Web API 支持 ASP.NET Routing 的全套路由功能,包括路由参数和约束。此外,使用简单的约定将操作映射到 HTTP 方法。
?Content negotiation: The client and server can work together to determine the right format for data being returned from a web API. ASP.NET Web API provides default support for XML, JSON, and Form URL-encoded formats and you can extend this support by adding your own formatters, or even replace the default content negotiation strategy.
? 内容协商:客户端和服务器可以共同确定从 Web API 返回的数据的正确格式。ASP.NET Web API 提供对 XML、JSON 和表单 URL 编码格式的默认支持,您可以通过添加自己的格式化程序来扩展这种支持,甚至替换默认的内容协商策略。
?Model binding and validation: Model binders provide an easy way to extract data from various parts of an HTTP request and convert those message parts into .NET objects which can be used by the Web API actions. Validation is also performed on action parameters based on data annotations.
? 模型绑定和验证:模型绑定器提供了一种从 HTTP 请求的各个部分提取数据并将这些消息部分转换为 Web API 操作可以使用的 .NET 对象的简单方法。还根据数据注释对动作参数执行验证。
?Filters: ASP.NET Web API supports filters including well-known filters such as the [Authorize] attribute. You can author and plug in your own filters for actions, authorization and exception handling.
? 过滤器:ASP.NET Web API 支持过滤器,包括众所周知的过滤器,例如 [Authorize] 属性。您可以为操作、授权和异常处理编写和插入您自己的过滤器。
?Query composition: Use the [Queryable] filter attribute on an action that returns IQueryable to enable support for querying your web API via the OData query conventions.
? 查询组合:在返回 IQueryable 的操作上使用 [Queryable] 过滤器属性,以启用对通过 OData 查询约定查询 Web API 的支持。
?Improved testability: Rather than setting HTTP details in static context objects, web API actions work with instances of HttpRequestMessage and HttpResponseMessage. Create a unit test project along with your Web API project to get started quickly writing unit tests for your Web API functionality.
? 改进的可测试性:Web API 操作不是在静态上下文对象中设置 HTTP 详细信息,而是与 HttpRequestMessage 和 HttpResponseMessage 的实例一起使用。创建一个单元测试项目以及您的 Web API 项目,以便快速开始为您的 Web API 功能编写单元测试。
?Code-based configuration: ASP.NET Web API configuration is accomplished solely through code, leaving your config files clean. Use the provide service locator pattern to configure extensibility points.
? 基于代码的配置:ASP.NET Web API 配置仅通过代码完成,使您的配置文件保持干净。使用提供服务定位器模式来配置扩展点。
?Improved support for Inversion of Control (IoC) containers: ASP.NET Web API provides great support for IoC containers through an improved dependency resolver abstraction
? 改进了对控制反转 (IoC) 容器的支持:ASP.NET Web API 通过改进的依赖解析器抽象为 IoC 容器提供了强大的支持
?Self-host: Web APIs can be hosted in your own process in addition to IIS while still using the full power of routes and other features of Web API.
? 自托管:除了 IIS 之外,Web API 还可以托管在您自己的进程中,同时仍然可以使用路由的全部功能和 Web API 的其他功能。
?Create custom help and test pages: You now can easily build custom help and test pages for your web APIs by using the new IApiExplorer service to get a complete runtime description of your web APIs.
? 创建自定义帮助和测试页面:您现在可以使用新的 IApiExplorer 服务为您的 Web API 轻松构建自定义帮助和测试页面,以获取您的 Web API 的完整运行时描述。
?Monitoring and diagnostics: ASP.NET Web API now provides light weight tracing infrastructure that makes it easy to integrate with existing logging solutions such as System.Diagnostics, ETW and third party logging frameworks. You can enable tracing by providing an ITraceWriter implementation and adding it to your web API configuration.
? 监视和诊断:ASP.NET Web API 现在提供轻量级跟踪基础结构,可以轻松与现有日志记录解决方案(例如 System.Diagnostics、ETW 和第三方日志记录框架)集成。您可以通过提供 ITraceWriter 实现并将其添加到您的 Web API 配置来启用跟踪。
?Link generation: Use the ASP.NET Web API UrlHelper to generate links to related resources in the same application.
? 链接生成:使用 ASP.NET Web API UrlHelper 生成指向同一应用程序中相关资源的链接。
?Web API project template: Select the new Web API project form the New MVC 4 Project wizard to quickly get up and running with ASP.NET Web API.
? Web API 项目模板:从新建 MVC 4 项目向导中选择新的 Web API 项目,以快速启动并运行 ASP.NET Web API。
?Scaffolding: Use the Add Controller dialog to quickly scaffold a web API controller based on an Entity Framework based model type.
? 脚手架:使用“添加控制器”对话框基于基于实体框架的模型类型快速搭建 Web API 控制器。
回答by Display Name
Copy and paste from Whats new in MVC4 - MVC3 Vs MVC4
从MVC4 中的新功能复制和粘贴- MVC3 Vs MVC4
Whats new in MVC4 - MVC3 Vs MVC4
MVC4 中的新功能 - MVC3 与 MVC4
Enhancements to Default Project Templates
默认项目模板的增强
The template that is used to create new ASP.NET MVC 4 projects has been updated to create a more modern-looking website
用于创建新 ASP.NET MVC 4 项目的模板已更新,以创建外观更现代的网站
Mobile Project Template
移动项目模板
If you're starting a new project and want to create a site specifically for mobile and tablet browsers, you can use the new Mobile Application project template. This is based on jQuery Mobile, an open-source library for building touch-optimized UI
如果您正在开始一个新项目并想要专门为移动和平板电脑浏览器创建一个站点,您可以使用新的移动应用程序项目模板。这是基于 jQuery Mobile,一个用于构建触摸优化 UI 的开源库
Display Modes
显示模式
The new Display Modes feature lets an application select views depending on the browser that's making the request. For example, if a desktop browser requests the Home page, the application might use the Views\Home\Index.cshtmltemplate. If a mobile browser requests the Home page, the application might return the Views\Home\Index.mobile.cshtmltemplate.
新的显示模式功能允许应用程序根据发出请求的浏览器选择视图。例如,如果桌面浏览器请求主页,应用程序可能会使用Views\Home\Index.cshtml模板。如果移动浏览器请求主页,应用程序可能会返回Views\Home\Index.mobile.cshtml模板。
DisplayModes.Modes.Insert(0, new DefaultDisplayMode("iPhone")
{
ContextCondition = (context => context.Request.UserAgent.IndexOf
("iPhone", StringComparison.OrdinalIgnoreCase) >= 0)
});
jQuery Mobile, the View Switcher, and Browser Overriding
jQuery Mobile、视图切换器和浏览器覆盖
jQuery Mobile is an open source library for building touch-optimized web UI. If you want to use jQuery Mobile with an ASP.NET MVC 4 application, you can download and install a NuGet package that helps you get started. To install it from the Visual Studio Package Manager Console, type the following command: Install-Package jQuery.Mobile.MVC This installs jQuery Mobile and some helper files, including the following: Views/Shared/Layout.Mobile.cshtml, which is a jQuery Mobile-based layout. A view-switcher component, which consists of the Views/Shared/ViewSwitcher.cshtml partial view and the ViewSwitcherController.cs controller. After you install the package, run your application using a mobile browser (or equivalent, like the Firefox User Agent Switcher add-on). You'll see that your pages look quite different, because jQuery Mobile handles layout and styling. To take advantage of this, you can do the following If visitors click the link, they're switched to the desktop version of the same page. Because your desktop layout will not include a view switcher by default, visitors won't have a way to get to mobile mode. To enable this, add the following reference to _ViewSwitcher to your desktop layout, just inside the element:
jQuery Mobile 是一个开源库,用于构建针对触摸优化的 Web UI。如果您想将 jQuery Mobile 与 ASP.NET MVC 4 应用程序一起使用,您可以下载并安装可帮助您入门的 NuGet 包。要从 Visual Studio 包管理器控制台安装它,请键入以下命令: Install-Package jQuery.Mobile.MVC 这将安装 jQuery Mobile 和一些帮助文件,包括以下内容:Views/Shared/Layout.Mobile.cshtml,这是一个基于 jQuery Mobile 的布局。一个视图切换器组件,由 Views/Shared/ViewSwitcher.cshtml 局部视图和 ViewSwitcherController.cs 控制器组成。安装软件包后,使用移动浏览器(或等效物,如 Firefox User Agent Switcher 附加组件)运行您的应用程序。你会看到你的页面看起来很不一样,因为 jQuery Mobile 处理布局和样式。要利用这一点,您可以执行以下操作 如果访问者单击该链接,他们将切换到同一页面的桌面版本。由于您的桌面布局默认不包含视图切换器,因此访问者将无法进入移动模式。要启用此功能,请将以下对 _ViewSwitcher 的引用添加到您的桌面布局中,就在元素内部:
@Html.Partial("_ViewSwitcher")
... Browser Overriding is a core feature of ASP.NET MVC 4 and is available even if you don't install the jQuery.Mobile.MVC package. However, it affects only view, layout, and partial-view selection — it does not affect any other ASP.NET feature that depends on the Request.Browser object.
... 浏览器覆盖是 ASP.NET MVC 4 的核心功能,即使您不安装 jQuery.Mobile.MVC 包也可用。但是,它只影响视图、布局和部分视图选择——它不会影响依赖于 Request.Browser 对象的任何其他 ASP.NET 功能。
Recipes for Code Generation in Visual Studio
在 Visual Studio 中生成代码的秘诀
The new Recipes feature enables Visual Studio to generate solution-specific code based on packages that you can install using NuGet. The Recipes framework makes it easy for developers to write code-generation plugins, which you can also use to replace the built-in code generators for Add Area, Add Controller, and Add View. Because recipes are deployed as NuGet packages, they can easily be checked into source control and shared with all developers on the project automatically. They are also available on a per-solution basis.
新的 Recipes 功能使 Visual Studio 能够根据可以使用 NuGet 安装的包生成解决方案特定的代码。Recipes 框架使开发人员可以轻松编写代码生成插件,您还可以使用这些插件替换 Add Area、Add Controller 和 Add View 的内置代码生成器。由于配方被部署为 NuGet 包,因此它们可以轻松地签入源代码管理并自动与项目中的所有开发人员共享。它们也可在每个解决方案的基础上使用。
Task Support for Asynchronous Controllers
异步控制器的任务支持
You can now write asynchronous action methods as single methods that return an object of type Task or Task.
您现在可以将异步操作方法编写为返回 Task 或 Task 类型对象的单个方法。
For example, if you're using Visual C# 5 (or using the Async CTP), you can create an asynchronous action method that looks like the following:
例如,如果您使用的是 Visual C# 5(或使用 Async CTP),您可以创建一个如下所示的异步操作方法:
public async Task Index(string city) {
var newsService = new NewsService();
var sportsService = new SportsService();
return View("Common", new PortalViewModel {
NewsHeadlines = await newsService.GetHeadlinesAsync(),
SportsScores = await sportsService.GetScoresAsync()
});
}
In the previous action method, the calls to newsService.GetHeadlinesAsync and sportsService.GetScoresAsync are called asynchronously and do not block a thread from the thread pool.
在前面的操作方法中,对 newsService.GetHeadlinesAsync 和sportsService.GetScoresAsync 的调用是异步调用的,不会阻塞线程池中的线程。
Asynchronous action methods that return Task instances can also support timeouts. To make your action method cancellable, add a parameter of type CancellationToken to the action method signature. The following example shows an asynchronous action method that has a timeout of 2500 milliseconds and that displays a TimedOut view to the client if a timeout occurs.
返回 Task 实例的异步操作方法也可以支持超时。要使您的操作方法可取消,请将类型为 CancellationToken 的参数添加到操作方法签名中。以下示例显示了一个异步操作方法,该方法的超时时间为 2500 毫秒,并且在发生超时时向客户端显示 TimedOut 视图。
[AsyncTimeout(2500)]
[HandleError(ExceptionType = typeof(TaskCanceledException), View = "TimedOut")]
public async Task Index(string city, CancellationToken cancellationToken) {
var newsService = new NewsService();
var sportsService = new SportsService();
return View("Common", new PortalViewModel {
NewsHeadlines = await newsService.GetHeadlinesAsync(cancellationToken),
SportsScores = await sportsService.GetScoresAsync(cancellationToken)
});
}
Hope this helps. Thanks
希望这可以帮助。谢谢
回答by Afazal
Please go through the URL for all MVC 4 new features
回答by Mukesh Kumar
MVC 3
MVC 3
- Integrated Scaffolding system extensible via NuGet
- HTML 5 enabled project templates
- Expressive Views including the new Razor View Engine
- Powerful hooks with Dependency Injection and Global Action Filters
- Rich JavaScript support with unobtrusive JavaScript, jQuery Validation, and JSON binding
- 可通过 NuGet 扩展的集成脚手架系统
- 支持 HTML 5 的项目模板
- 富有表现力的视图,包括新的 Razor 视图引擎
- 具有依赖注入和全局动作过滤器的强大钩子
- 通过不显眼的 JavaScript、jQuery 验证和 JSON 绑定提供丰富的 JavaScript 支持
MVC 4
MVC 4
- ASP.NET Web API
- Refreshed and modernized default project templates
- New mobile project template
- Many new features to support mobile apps
- Enhanced support for asynchronous methods
- ASP.NET Web API
- 更新和现代化的默认项目模板
- 新的移动项目模板
- 许多支持移动应用程序的新功能
- 增强对异步方法的支持
Ref : http://dotnet-developers-cafe.blogspot.in/2013/09/difference-between-aspnet-mvc-3-and-mvc.html
参考:http: //dotnet-developers-cafe.blogspot.in/2013/09/difference-between-aspnet-mvc-3-and-mvc.html
回答by Rahul
Please check the below links to find out the differences.
请检查以下链接以找出差异。
http://forums.asp.net/t/1753017.aspx
http://forums.asp.net/t/1753017.aspx
http://www.askamoeba.com/Answer/130/Whats-new-in-MVC4-MVC3-Vs-MVC4
http://www.askamoeba.com/Answer/130/Whats-new-in-MVC4-MVC3-Vs-MVC4
http://forums.asp.net/t/1918539.aspx?Main+difference+between+regular+webapplication+mvc3+and+mvc4+
http://forums.asp.net/t/1918539.aspx?Main+difference+between+regular+webapplication+mvc3+and+mvc4+
回答by Abhishek Gahlout
One of the important feature introduced in MVC 4.0 was of Asynchronous controllers which enables to write the asynchronous action methods. Asynchronous controller allows an operation to get performed without making the working thread idle.
MVC 4.0 中引入的重要特性之一是异步控制器,它能够编写异步操作方法。异步控制器允许在不使工作线程空闲的情况下执行操作。
When an asynchronous action is invoked, the following steps occur:
调用异步操作时,会发生以下步骤:
The Web server gets a thread from the thread pool (the worker thread) and schedules it to handle an incoming request. This worker thread initiates an asynchronous operation. The worker thread is returned to the thread pool to service another Web request. When the asynchronous operation is complete, it notifies ASP.NET. The Web server gets a worker thread from the thread pool (which might be a different thread from the thread that started the asynchronous operation) to process the remainder of the request, including rendering the response.
Web 服务器从线程池(工作线程)中获取一个线程并调度它来处理传入的请求。此工作线程启动异步操作。工作线程返回到线程池以服务另一个 Web 请求。当异步操作完成时,它会通知 ASP.NET。Web 服务器从线程池中获取一个工作线程(它可能与启动异步操作的线程不同)来处理请求的其余部分,包括呈现响应。
Converting Synchronous Action Methods to Asynchronous Action Methods
将同步操作方法转换为异步操作方法
Following is the example of synchronous action method and the its asynchronous equivalent version.
以下是同步操作方法及其异步等效版本的示例。
Synchronous Controller:
同步控制器:
public class TestController : Controller
{
public ActionResult Index()
{
return View();
}
}
Asynchronous variant of above operation:
上述操作的异步变体:
public class TestController : AsyncController
{
public void IndexAsync()
{
return View();
}
public ActionResult IndexCompleted()
{
return View();
}
}
Steps:
脚步:
Synchronous Controllers are the classes derived from the Controller class to implement an AsyncController instead of deriving the controller from Controller, derive it from AsyncController class. Controllers that derive from AsyncController enable ASP.NET to process asynchronous requests, and they can still service synchronous action methods.
Corresponding to the synchronous action method in Synchronous controller you need to create two methods for the action in asynchronous controller.First method that initiates the asynchronous process must have a name that consists of the action and the suffix "Async". The other method that is invoked when the asynchronous process finishes (the callback method) must have a name that consists of the action and the suffix "Completed".
In the above sample example, the Index action has been turned into two methods in asynchronous controller: IndexAsync and IndexCompleted.
The IndexAsync method returns void while the IndexCompleted method returns an ActionResult instance. Although the action consists of two methods, it is accessed using the same URL as for a synchronous action method (for example, Controller/Index).
同步控制器是从 Controller 类派生的类来实现 AsyncController,而不是从 Controller 派生控制器,从 AsyncController 类派生它。从 AsyncController 派生的控制器使 ASP.NET 能够处理异步请求,并且它们仍然可以为同步操作方法提供服务。
对应于同步控制器中的同步动作方法,你需要为异步控制器中的动作创建两个方法。第一个启动异步过程的方法必须有一个由动作和后缀“Async”组成的名称。异步进程完成时调用的另一个方法(回调方法)必须具有由操作和后缀“Completed”组成的名称。
在上面的示例示例中,Index 操作已转换为异步控制器中的两个方法:IndexAsync 和 IndexCompleted。
IndexAsync 方法返回 void,而 IndexCompleted 方法返回 ActionResult 实例。尽管操作由两个方法组成,但使用与同步操作方法相同的 URL 访问它(例如,控制器/索引)。
Note the following about asynchronous action methods:
请注意以下有关异步操作方法的信息:
If the action name is Sample, the framework will look for SampleAsync and SampleCompleted methods.
如果操作名称为 Sample,则框架将查找 SampleAsync 和 SampleCompleted 方法。
View pages should be named Sample.aspx rather than SampleAsync.aspx or SampleCompleted.aspx. (The action name is Sample, not SampleAsync)
视图页面应命名为 Sample.aspx 而不是 SampleAsync.aspx 或 SampleCompleted.aspx。(操作名称是 Sample,而不是 SampleAsync)
A controller cannot contain an asynchronous method named SampleAsync and a synchronous method named Sample. If it does, an AmbiguousMatchException exception is thrown because the SampleAsync action method and the Sample action method have the same request signature.
控制器不能包含名为 SampleAsync 的异步方法和名为 Sample 的同步方法。如果是,则会引发 AmbiguousMatchException 异常,因为 SampleAsync 操作方法和 Sample 操作方法具有相同的请求签名。
For more details click here : http://www.counsellingbyabhi.com/2014/05/asynchronous-controllers-in-aspnet-mvc.html
有关更多详细信息,请单击此处:http: //www.coun sellbyabhi.com/2014/05/asynchronous-controllers-in-aspnet-mvc.html

