Difference between ApiController and Controller in ASP.NET MVC
我一直在使用ASP.NET MVC 4 beta,现在我看到两种类型的控制器:
在什么情况下可以选择特定的控制器,我一点也不困惑。
例如:如果要返回视图,则必须使用
从现在开始,我们可以同时使用这两种控制器,请有人指出对应控制器的情况。
使用Controller渲染普通视图。 ApiController操作仅返回已序列化并发送到客户端的数据。
链接在这里
引用:
Note If you have worked with ASP.NET MVC, then you are already familiar with controllers. They work similarly in Web API, but controllers in Web API derive from the ApiController class instead of Controller class. The first major difference you will notice is that actions on Web API controllers do not return views, they return data.
ApiControllers专门用于返回数据。例如,他们负责透明地将数据序列化为客户端请求的格式。此外,它们默认情况下遵循不同的路由方案(例如:将URL映射到操作),并按惯例提供REST-ful API。
您可以使用控制器而不是带有some(?)手动编码的ApiController来执行任何操作。最后,两个控制器都基于ASP.NET基础。但是,如今,拥有REST-ful API已成为一种普遍要求,因此创建WebAPI的目的是简化此类API的实现。
在这两者之间做出决定是相当简单的:如果您正在编写基于HTML的Web /互联网/内联网应用程序-也许偶尔会在此不停地返回json的AJAX调用-坚持使用MVC / Controller。如果要为系统提供数据驱动/基于REST的接口,请使用WebAPI。当然,您可以将两者结合起来,让ApiController满足来自MVC页面的AJAX调用。
举一个真实的例子:我目前正在使用一个ERP系统,该系统为其实体提供REST-ful API。对于此API,WebAPI将是不错的选择。同时,ERP系统提供了高度AJAX认证的Web应用程序,您可以使用它来创建针对REST-ful API的查询。 Web应用程序本身可以实现为MVC应用程序,利用WebAPI来获取元数据等。
您宁愿编写和维护哪个?
ASP.NET MVC
1 2 3 4 5 6 7 | public class TweetsController : Controller { // GET: /Tweets/ [HttpGet] public ActionResult Index() { return Json(Twitter.GetTweets(), JsonRequestBehavior.AllowGet); } } |
ASP.NET Web API
1 2 3 4 5 6 | public class TweetsController : ApiController { // GET: /Api/Tweets/ public List<Tweet> Get() { return Twitter.GetTweets(); } } |
我喜欢ASP.NET Core的MVC6将这两种模式合并为一个事实,因为我经常需要同时支持这两种情况。确实可以调整任何标准MVC
迄今为止,我为使ASP.NET非核心Web应用程序中的问题最小化的最佳技术是将Web API包导入(并正确配置)到基于MVC的Web应用程序中,因此我可以兼得两者的优点worlds:
为此,您需要执行以下操作:
-
使用NuGet安装以下Web API软件包:
Microsoft.AspNet.WebApi.Core 和Microsoft.AspNet.WebApi.WebHost 。 -
将一个或多个ApiControllers添加到您的
/Controllers/ 文件夹。 -
将以下WebApiConfig.cs文件添加到您的
/App_Config/ 文件夹中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | using System.Web.Http; public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name:"DefaultApi", routeTemplate:"api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } } |
最后,您需要将上述类注册到您的Startup类(
启动文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public void Configuration(IAppBuilder app) { // Register Web API routing support before anything else GlobalConfiguration.Configure(WebApiConfig.Register); // The rest of your file goes there // ... AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); ConfigureAuth(app); // ... } |
Global.asax.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 | protected void Application_Start() { // Register Web API routing support before anything else GlobalConfiguration.Configure(WebApiConfig.Register); // The rest of your file goes there // ... AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); // ... } |
我在博客上写的这篇文章进一步说明了这种方法及其优缺点。
Web API中的每个方法都将返回数据(JSON),而不进行序列化。
但是,为了在MVC控制器中返回JSON数据,我们将返回的Action Result类型设置为JsonResult,并在对象上调用Json方法以确保将其包装在JSON中。
它几乎是一个,但是,MVC和API之间的名称,使用目的和透明度不同。
主要区别在于:Web API是针对任何客户端,任何设备的服务,而MVC Controller仅为其客户端提供服务。 相同,因为它是MVC平台。
在这两者之间做出决定是相当简单的:如果您正在编写基于HTML的Web /互联网/内联网应用程序-也许偶尔会在此不停地返回json的AJAX调用-坚持使用MVC / Controller。如果要为系统提供数据驱动/基于REST的接口,请使用WebAPI。当然,您可以将两者结合起来,让ApiController满足来自MVC页面的AJAX调用。
基本上,控制器用于mvc,而api-controller用于Rest-
您可以根据需要在同一程序中同时使用这两种API