API控制器的单独文件夹,并在ASP.NET Core中查看控制器

发布于 2025-02-13 18:32:26 字数 674 浏览 1 评论 0原文

我正在尝试使用现有的.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

£噩梦荏苒 2025-02-20 18:32:27

您只需将

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

添加[route(“ API/[Controller]/[action]/{id?}?}”)]在您的API控制器上,它可以很好地工作。

我测试如下:

另外,您可以尝试

 app.MapControllerRoute(
        name: "default",
        pattern: "~/api/{controller}/{action}/{id?}",
        new { controller = "...", action = "...." });

You could just keep

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

add[Route("api/[controller]/[action]/{id?}")] on your api controller and it would work well.

I tested as below:
enter image description here

Also,you could try with

 app.MapControllerRoute(
        name: "default",
        pattern: "~/api/{controller}/{action}/{id?}",
        new { controller = "...", action = "...." });
猥︴琐丶欲为 2025-02-20 18:32:27

我决定复兴这篇文章,以分享我的设置最终的内容:

我制作了一个抽象控制器apibasecontroller,以帮助将/api路由应用于我的/控制器/API文件夹,现在此解决方案不仅可以通过在/Controllers/api文件夹中使用控制器来实现它这每个端点或每个控制器上的路径。

在基本控制器上,我将路由属性添加到了类本身:

[Route("/Api/[controller]/[action]")]
public abstract class ApiBaseController : ControllerBase { }

中继承的API文件夹中拥有的每个控制器

public class DataController : ApiBaseController
{
  [HttpGet]
  public IActionResult Data()
  {
    return new JsonResult(new {id=1});
  }
}

然后,我从该类 。在上面的示例中,该路由为/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:

[Route("/Api/[controller]/[action]")]
public abstract class ApiBaseController : ControllerBase { }

Then, each controller I have in the Api folder I inherit from that class:

public class DataController : ApiBaseController
{
  [HttpGet]
  public IActionResult Data()
  {
    return new JsonResult(new {id=1});
  }
}

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 from ApiBaseController use the default route setup by using app.MapDefaultControllerRoute();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文