简体   繁体   English

.net 3.0 中的路由 - 路由参数和操作参数

[英]routing in .net 3.0 - route parameters and action parameters

.net core 3.0 web app with razorpages and api .net 核心 3.0 web 应用程序与 razorpages 和 api

My api controllers are in a folder "/api" and one of them has a default Get method that I want to call with both a route parameter AND 2 other values我的 api 控制器位于文件夹“/api”中,其中一个具有默认的 Get 方法,我想使用路由参数和 2 个其他值调用该方法

[HttpGet("{customer}")]
public IActionResult Get(string customer, string size, string color)

Trying to route using a parameter to a path like this尝试使用参数路由到这样的路径
http get localhost://site-root/api/mycontroller/ABC?size=val1&color=val2 http 获取 localhost://site-root/api/mycontroller/ABC?size=val1&color=val2
Where "ABC" would get mapped to a route parameter "customer" in my controller but this wont route.在我的 controller 中,“ABC”将映射到路由参数“customer”,但这不会路由。

... in configured routing in Startup ...在启动中配置的路由中

 app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
        endpoints.MapControllerRoute(name: "api", pattern: "api/[controller]/[action]");
        endpoints.MapRazorPages();
    });

The problem seems to be in how you are registering the route.问题似乎在于您如何注册路线。 It should be:它应该是:

 app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
        endpoints.MapControllerRoute(name: "api", pattern: "api/[controller]");
        endpoints.MapRazorPages();
    });

In your code, you are expecting an "[action]", therefore, the "ABC" text is being mapped as an action and not as an argument to the function.在您的代码中,您需要一个“[action]”,因此,“ABC”文本被映射为一个操作,而不是 function 的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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