API控制器的单独文件夹,并在ASP.NET Core中查看控制器
我正在尝试使用现有的.NET 6 ASP.NET核心项目,并将某些数据逻辑与视图逻辑分开。我有一堆视图控制器,这些控制器也充满了数据端点,我想将其移出到单独的控制器。我试图逐步执行此操作(因为这是一个大项目),而无需进行完整的转换。
最终目标是缩小视图控制器的大小,并将某些共享端点组合在视图控制器之间,以便数据专门来自API端点,而不是视图控制器路由的根端点。
我正在尝试使用目前工作的文件夹结构是这样的:
Controllers
-> Api
--> Data1Controller
-> View1Controller
我无法弄清楚如何为API子文件夹中的所有控制器获得路由设置。
编辑:
我尝试使用区域来执行此操作,但仍然无法使其工作:
program.cs中的路由代码:
app.UseRouting();
app.MapAreaControllerRoute("Api", "Api", "Api/{controller}/{action}/{id?}");
app.MapDefaultControllerRoute();
我设置了一个带有API区域的简单控制器(DataController)和 /api /Data返回404
I am trying to take an existing .NET 6 ASP.NET Core project and separate out some of the data logic from the view logic. I have a bunch of view controllers that are also filled with data endpoints that I would like to move out to separate controllers. I am attempting to do this incrementally (since it is a large project) without having to do a full conversion.
The end goal is to shrink the size of the view controllers and combine some of the shared endpoints among the view controllers so that data comes specifically from an api endpoint not the root endpoint the views controllers are routing.
The folder structure I am trying to get to work currently is something like this:
Controllers
-> Api
--> Data1Controller
-> View1Controller
I haven't been able to figure out how to get the routing setup for all controllers in Api subfolder.
Edit:
I tried using Areas to do this and still wasn't able to get it to work:
routing code in program.cs:
app.UseRouting();
app.MapAreaControllerRoute("Api", "Api", "Api/{controller}/{action}/{id?}");
app.MapDefaultControllerRoute();
I set up a simple controller (DataController) with Api Area and /api/data returns 404
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需将
添加
[route(“ API/[Controller]/[action]/{id?}?}”)]
在您的API控制器上,它可以很好地工作。我测试如下:

另外,您可以尝试
You could just keep
add
[Route("api/[controller]/[action]/{id?}")]
on your api controller and it would work well.I tested as below:

Also,you could try with
我决定复兴这篇文章,以分享我的设置最终的内容:
我制作了一个抽象控制器
apibasecontroller
,以帮助将/api
路由应用于我的/控制器/API
文件夹,现在此解决方案不仅可以通过在/Controllers/api
文件夹中使用控制器来实现它这每个端点或每个控制器上的路径。在基本控制器上,我将路由属性添加到了类本身:
中继承的API文件夹中拥有的每个控制器
然后,我从该类 。在上面的示例中,该路由为
/api/data/data
和我所有的所有其他控制器都不从apibaSeconTroller继承
使用app.mapdefeault.mapdefaultcontrollerrerroute使用默认路由设置();
I decided to revist this post to share what my setup ended up being:
I made an abstract controller
ApiBaseController
to help apply the/Api
route to everything in my/Controllers/Api
folder, now this solution doesn't make it just by having the controller in the/Controllers/Api
folder but it does make it so I don't have to manually specify the path on each endpoint or each controller.On the base controller, I added the route Attribute to the class itself:
Then, each controller I have in the Api folder I inherit from that class:
This allows me to not specify routes for every api controller but just add inheritance to the abstract class. In the above example, the route is
/Api/Data/Data
and all my other controllers not inheriting fromApiBaseController
use the default route setup by usingapp.MapDefaultControllerRoute();