简体   繁体   中英

Can i add API controller in Areas in MVC

如何在Mvc。区域中添加api控制器并访问它

Web API does not have any concept of areas which is a standard "MVC" feature.

You can place web API controllers anywhere in your solution and they will simply work regardless of their location in the solution structure.

There is no folder based convention for WEB API controllers, always remember that WEB API controllers are different to standard MVC controllers in this way.

You can add your MVC controllers to any folder you like (including those folders relating to your areas).

Its simple. Create yours MVC project. You can add your api controller to your controller by right click on controllers folder select add you will find many option choose Web API Controller class. I think this will help

You can add a API controller anywhere (in an area) but to access it, by default, always use the same code

 http://www.yourapp.com/api/yourcontroller

It didn't work when I tried http://www.yourapp.com/MYAREA/api/yourcontroller

Yes, Easily

1-Create a folder named Api (for example) inside your area's controller folder

2- Create an Api controller inside Api folder

3- Go to your [AreaName] AreaRegistration.cs File

4-Add new route into RegisterArea method

ex:

context.Routes.MapHttpRoute( name: "MyAreaApi", routeTemplate: "MyAreaApi/api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } )

5-Finish

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