简体   繁体   English

在Web API Razor中处理视图路由的最佳方法是什么

[英]What is the best way to handle the view routing in Web API Razor

I have this doubt, because always when I create a Web API project I use the API controllers to get data and I create a normal controller where I define all my methods that returns ActionResult to redirect to the views and I am not sure if this is the best way. 我有这个疑问,因为总是在创建Web API项目时使用API​​控制器来获取数据,并创建一个普通的控制器,在该控制器中定义所有返回ActionResult的方法以重定向到视图,并且不确定是否最好的方法。

What is the correct way to do this? 正确的方法是什么?

Web API is not for views. Web API不适用于视图。 API stands for application programming interface - that is, there is no user interface (views) that a user can interact with. API代表应用程序编程接口 -即,没有用户可以与之交互的用户界面(视图)。

There is another framework with view functionality built-in: ASP.NET MVC . 还有一个内置视图功能的框架: ASP.NET MVC It will even run side-by-side within your Web API project - see How to add MVC to existing web API project . 它甚至可以在您的Web API项目中并行运行-请参阅如何将MVC添加到现有的Web API项目

So in short: 简而言之:

  1. To return views, use an MVC controller (inherited from System.Web.Mvc.Controller ) 要返回视图,请使用MVC控制器(从System.Web.Mvc.Controller继承)
  2. Use a Web API controller (inherited from System.Web.Http.ApiController ) when you want to return data to an application or browser. 要将数据返回到应用程序或浏览器时,请使用Web API控制器(从System.Web.Http.ApiController继承)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 与 Web API 相同的 Razor 页面路由 - Razor Page Routing in the same way as in Web API 在Web应用程序中处理DateTime的最佳方法是什么? - What is the best way to handle DateTime in a Web Application? 版本WCF Web API的最佳方法是什么? - What is the best way to version a WCF Web API? 处理asp.net Web Api中未经身份验证的请求的最佳方法 - Best way to handle unauthenticated request in asp.net Web Api 使用外部 API 和处理服务停机情况的最佳方法是什么 - what is the best way to consume external API and handle service down cases 在控制台应用程序中使用 Razor 的最佳方式是什么 - What is the best way to use Razor in a console application 使用Razor视图引擎将MVC 2项目迁移到MVC 3的最佳方法是什么? - What is the best way to migrate a MVC 2 project to MVC 3 using the Razor view engine? 制作每次选择过滤器都会更改的内容的剃刀视图的最佳方法是什么? - What is the best way to make a razor view with content that changes every time you choose a filter? 在Mvc 3 razor中查看在模型中基于Null有条件地渲染html的最佳方法是什么 - In Mvc 3 razor view what is the best way to conditionally render html based on Nulls in the model 将部分剃刀视图公开到Web API控制器中 - Expose partial razor view into web api controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM