简体   繁体   中英

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.

What is the correct way to do this?

Web API is not for views. API stands for application programming interface - that is, there is no user interface (views) that a user can interact with.

There is another framework with view functionality built-in: 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 .

So in short:

  1. To return views, use an MVC controller (inherited from 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM