How to convert an empty MVC project to Web API?

I created "Empty" for MVC 4. After coding some things, I realized that the Web API project is better. I don't want to start, so is there a way to convert it to a web API project? Below is a screenshot of what I mean. Any help would be greatly appreciated.

enter image description here

+4
source share
3 answers

You can add a web API controller to your MVC project. In Solution Explorer, right-click the Controllers folder, select Add Controller, and then select Web API Controller.

The only thing you need is routing - I believe that the "empty" MVC 4 project by default includes a web API route, so you should be fine. Find something like:

routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); 

But if you want to convert an existing MVC controller to a web API controller, there is no automatic way to do this.

+6
source

The Web API is only better if you want to provide data. If you need to visualize views (Razor or Web Forms), use ASP.NET MVC.

There are no automatic conversions as they do different things.

+1
source

If you really want to switch to a web API project, the simplest thing is to simply create a new empty web interface project. Then copy all the files from the old project, delete the old project and rename the new one.

Another option that may require more effort is to create a new empty web interface project. Then open the project file in a text editor and compare it with the old project file and make the necessary changes to the unprocessed file. All of this is in XML, so it shouldn't be that complicated.

+1
source

Source: https://habr.com/ru/post/1412081/


All Articles