我有一个带有 Angular 前端的 MVC 5 应用程序。
我在调用无参数端点时遇到问题。
MVC 应用程序有一个控制器,Angular 应用程序调用它来获取数据。
例如,我可以调用定义为的端点:
[HttpGet]
[Route("api/MyApplication/GetData/{id}")]
public ActionResult GetData(int id)
通过在 Angular 中使用以下内容
const endpoint = 'localhost:0000/api/MyApplication/GetData' + '/' + 0);
return this.httpClient.get<JSON>(endpoint);
但是,如果我将端点定义为
[HttpGet]
[Route("api/MyApplication/GetData/")]
public ActionResult GetData()
并在 Angular 中使用以下内容
const endpoint = 'localhost:0000/api/MyApplication/GetData');
return this.httpClient.get<JSON>(endpoint);
我收到 404 错误。从 Postman 调用时也是如此
你能建议我如何克服这个吗?
只有一个端点带有“.../GetData/”,没有最后一个正斜杠:“.../GetData”。根本没有匹配的路线。
如果你想要一个无参数端点,只需去掉最后一个正斜杠,就像这样:
[HttpGet]
[Route("api/MyApplication/GetData")]
public ActionResult GetData()
如果您想更多地控制随请求发送的 URL 参数,您可以查看此链接。