VJA*_*JAI 323 asp.net-mvc asp.net-web-api
我一直在玩ASP.NET MVC 4 beta,我现在看到两种类型的控制器:ApiController
和Controller
.
我对可以选择特定控制器的情况感到困惑.
例如:如果我想返回一个视图,那么我要使用ApiController
还是普通的Controller
?我知道WCF Web API现在已与MVC集成.
从现在我们可以使用两个控制器可以有人请指出在哪种情况下去相应的控制器.
And*_*ker 333
使用Controller渲染普通视图.ApiController操作仅返回序列化并发送到客户端的数据.
引用:
注意如果您使用过ASP.NET MVC,那么您已经熟悉了控制器.它们在Web API中的工作方式类似,但Web API中的控制器派生自ApiController类而不是Controller类.您将注意到的第一个主要区别是Web API控制器上的操作不返回视图,它们返回数据.
ApiControllers专门用于返回数据.例如,它们负责将数据透明地序列化为客户端请求的格式.此外,它们默认遵循不同的路由方案(如:将URL映射到操作),按惯例提供REST-ful API.
您可以使用Controller而不是带有一些(?)手动编码的ApiController来执行任何操作.最后,两个控制器都建立在ASP.NET基础之上.但是,拥有REST-ful API是如此普遍的要求,即创建WebAPI是为了简化这种API的实现.
在两者之间做出决定相当简单:如果你正在编写一个基于HTML的web/internet/intranet应用程序 - 可能偶尔会在这里和那里返回json的AJAX调用 - 坚持使用MVC/Controller.如果要为系统提供数据驱动/ REST-ful接口,请使用WebAPI.当然,您可以将两者结合起来,从MVC页面获得ApiController来处理AJAX调用.
举一个现实世界的例子:我目前正在使用ERP系统,为其实体提供REST-ful API.对于此API,WebAPI将是一个很好的候选者.同时,ERP系统提供了一个高度AJAX-ified的Web应用程序,您可以使用它来为REST-ful API创建查询.Web应用程序本身可以实现为MVC应用程序,利用WebAPI来获取元数据等.
Man*_*ain 186
你宁愿写作和维护哪个?
ASP.NET MVC
public class TweetsController : Controller {
// GET: /Tweets/
[HttpGet]
public ActionResult Index() {
return Json(Twitter.GetTweets(), JsonRequestBehavior.AllowGet);
}
}
Run Code Online (Sandbox Code Playgroud)
ASP.NET Web API
public class TweetsController : ApiController {
// GET: /Api/Tweets/
public List<Tweet> Get() {
return Twitter.GetTweets();
}
}
Run Code Online (Sandbox Code Playgroud)
Dar*_*eal 27
我喜欢ASP.NET Core的MVC6将这两种模式合并为一种这一事实,因为我经常需要支持这两种模式.虽然你可以调整任何标准的MVC Controller
(和/或开发自己的ActionResult
类)来行动和行为就像是一样ApiController
,但是维护和测试可能非常困难:最重要的是,让控制器方法ActionResult
与其他方法混合使用IHttpActionResult
从开发人员的角度来看,返回原始/序列化/ 数据可能会非常混乱,特别是如果您不是单独工作并且需要让其他开发人员加快这种混合方法的速度.
到目前为止,我在ASP.NET非核心Web应用程序中最小化该问题的最佳技术是将Web API包导入(并正确配置)到基于MVC的Web应用程序中,因此我可以充分利用这两者世界:Controllers
视图,ApiControllers
数据.
为此,您需要执行以下操作:
Microsoft.AspNet.WebApi.Core
和Microsoft.AspNet.WebApi.WebHost
./Controllers/
文件夹中./App_Config/
文件夹: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 }
);
}
}
Run Code Online (Sandbox Code Playgroud)
最后,您需要将上述类注册到您的Startup类(或者,Startup.cs
或者Global.asax.cs
,取决于您是否使用OWIN Startup模板).
Startup.cs
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);
// ...
}
Run Code Online (Sandbox Code Playgroud)
的Global.asax.cs
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);
// ...
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
207175 次 |
最近记录: |