gpt4 book ai didi

c# - 从一个 API 调用方法已完成,但在第二个 API 上面临 Not found 错误

转载 作者:太空宇宙 更新时间:2023-11-03 15:23:46 27 4
gpt4 key购买 nike

我有两个 Wep API。我已经使用一个例如完成了 CRUD 操作。顾客。但是当我构建另一个类似的 Web API 并调用一个方法时,它显示:

{,…} Message: "No HTTP resource was found that matches the request URI http://localhost:23995/Product/Insert'."

MessageDetail: "No route providing a controller name was found to match request URI '[[same link as above here]]'"

这是我的 JS 调用方法:

$scope.Insert = function () {    

$http({
method: 'post',
url: 'http://localhost:23995/Product/Insert',
data: JSON.stringify($scope.Product)
}).then(function (response) {
alert("chec");

});
}

在产品 Controller 中

// Insert 
[HttpPost]
[Route("{controller}/Insert")]
public string Insert([FromBody] Product newProd) {
newProd.Insert();
return newProd.DbResponse;
}

在供应商 Controller 中

// Insert 
[HttpPost]
[Route("{controller}/Insert")]
public string Insert([FromBody] Product newProd) {
newProd.Insert();
return newProd.DbResponse;
}

最佳答案

假设您已经启用了属性路由。

Attribute Routing in ASP.NET Web API 2

To enable attribute routing, call MapHttpAttributeRoutes during configuration. This extension method is defined in the System.Web.Http.HttpConfigurationExtensions class.

using System.Web.Http;

namespace WebApplication
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API routes
config.MapHttpAttributeRoutes();

// Other Web API configuration not shown.
}
}
}

并假设在给定的路线上出现错误

http://localhost:23995/Product/Insert

您的产品 Controller 应该看起来像这样。

[RoutePrefix("product")]
public class ProductController : ApiController {
// ... other code removed for brevity

// Insert
// eg: POST /product/insert
[HttpPost]
[Route("insert")]
public string Insert([FromBody] Product newProd) {...}
}

和你的供应商 Controller 看起来非常相似

[RoutePrefix("supplier")]
public class SupplierController : ApiController {
// ... other code removed for brevity

// Insert
// eg: POST /supplier/insert
[HttpPost]
[Route("insert")]
public string Insert([FromBody] Product newProd) {...}
}

你调用 JavaScript 应该能够正确调用所需的方法

关于c# - 从一个 API 调用方法已完成,但在第二个 API 上面临 Not found 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36203429/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com