当我使用Visual Studio中的默认模板在新设置的WebApi项目的帐户控制器中有多个动作方法具有相同的谓词,即POST时,我会得到一个异常。以下是关于VS的信息:
Microsoft Visual Studio Enterprise 2019
Version 16.3.10
VisualStudio.16.Release/16.3.10+29519.87下面是我得到的异常:
InvalidOperationException: The method 'post' on path '/api/Account' is registered multiple times.下面是启动文件中的默认路由设置:
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });我也尝试了以下方法,但没有任何帮助。
endpoints.MapControllerRoute(
name: "api",
pattern: "{controller}/{id?}");和
endpoints.MapControllerRoute(
name: "api",
pattern: "{controller}/{action}/{id?}");我也在官方文档中读到了端点路由,但我不能弄清楚我遗漏了什么。感谢您的帮助。
发布于 2019-12-02 07:11:15
您需要传入模板名称,即[HttpPost("my-other-route")]。
发布于 2019-12-02 11:13:17
尝试更改Controller的Route属性,如下所示
[Route("api/[controller]/[action]")]
[ApiController]
public class StudentsController : ControllerBase
{ ... }您可以使用请求路由调用PostStudent操作,如https://localhost:44346/api/students/postStudent,并使用请求路由调用PostStudentList操作,如https://localhost:44346/api/students/postStudentList。
https://stackoverflow.com/questions/59130379
复制相似问题