404 onWeb API 带参数的路由
404 onWeb API routes with parameters
我被这个错误难住了。
我有一个 API 控制器,如下所示
using Cousant.InvestNow.Web.Areas.Customer.Models;
using Cousant.InvestNow.Web.Controllers;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TheOakSoft.ApiClient;
namespace Cousant.InvestNow.Web.Areas.Customer.Controllers
{
[Authorize]
[RoutePrefix("customer/api/abl")]
public class AssetBackedLendingApiController : BaseWebApiController
{
[HttpGet, Route("my-loans/{customer_id}")]
public HttpResponseMessage GetMyLoans(string customerId)
{
return HandleResponseException(response =>
{
var apiClient = fluentApiClient.NewGetRequest("investnow", "my-loans").AddQuery("customer_id", $"{customerId}");
var resp = apiClient.GetResponse();
response.StatusCode = resp.Success && resp.Data != null ? HttpStatusCode.OK : HttpStatusCode.BadRequest;
response.Content = new ObjectContent<ApiResponse>(resp, GetMediaTypeFormatter());
});
}
[HttpGet, Route("get-loan-detail/{application_id:int}")]
public HttpResponseMessage GetLoanDetails(int applicationId)
{
return HandleResponseException(response =>
{
var apiClient = fluentApiClient.NewGetRequest("investnow", "get-loan-detail").AddQuery("application_id", $"{applicationId}");
var resp = apiClient.GetResponse();
response.StatusCode = resp.Success && resp.Data != null ? HttpStatusCode.OK : HttpStatusCode.BadRequest;
response.Content = new ObjectContent<ApiResponse>(resp, GetMediaTypeFormatter());
});
}
}
}
当我从 Postman 调用端点时,它有效。
但是当我尝试从 JavaScript 调用相同的端点时,我每次都会收到 404 错误。我正在处理遗留应用程序,之前我已经编写过这样的端点,但我不明白为什么它不起作用。请注意,这仅发生在需要参数的端点上。只要没有传递路由参数,同一控制器中的其他端点就可以正常工作。即使 POST 请求工作 fine.Just 带参数的 GET 请求也是问题。
在我的 Javascript 代码中,我用 Axios 调用 enpoints
getLoanDetails: function () {
var self = this;
$.LoadingOverlay("show");
axios.get('/customer/api/abl/get-loan-detail', {
params: {
application_id: 50
}
})
.then(function (response) {
})
.catch(function (error) {
})
.then(function () {
});
},
getLoanHistory2: function () {
var self = this;
axios.get('/customer/api/abl/my-loans', {
params: {
customer_id: self.customerId
}
})
.then(function (response) {
})
.catch(function (error) {
})
.then(function () {
});
}
axios-get's params 添加参数作为查询字符串
所以axios.get('/user?ID=12345')
等同于
axios.get('/user', {
params: {
ID: 12345
}
})
您的代码已将参数定义为路由而不是查询参数,因此
axios.get('/customer/api/abl/get-loan-detail', {
params: {
application_id: 50
}
})
正在解析为 /customer/api/abl/get-loan-detail?application_id=50
而不是 /customer/api/abl/get-loan-detail/50
这就是你得到 404 的原因。
您可以按如下方式创建 url
axios.get(`/customer/api/abl/my-loans/${self.customerId}`).then
我被这个错误难住了。 我有一个 API 控制器,如下所示
using Cousant.InvestNow.Web.Areas.Customer.Models;
using Cousant.InvestNow.Web.Controllers;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TheOakSoft.ApiClient;
namespace Cousant.InvestNow.Web.Areas.Customer.Controllers
{
[Authorize]
[RoutePrefix("customer/api/abl")]
public class AssetBackedLendingApiController : BaseWebApiController
{
[HttpGet, Route("my-loans/{customer_id}")]
public HttpResponseMessage GetMyLoans(string customerId)
{
return HandleResponseException(response =>
{
var apiClient = fluentApiClient.NewGetRequest("investnow", "my-loans").AddQuery("customer_id", $"{customerId}");
var resp = apiClient.GetResponse();
response.StatusCode = resp.Success && resp.Data != null ? HttpStatusCode.OK : HttpStatusCode.BadRequest;
response.Content = new ObjectContent<ApiResponse>(resp, GetMediaTypeFormatter());
});
}
[HttpGet, Route("get-loan-detail/{application_id:int}")]
public HttpResponseMessage GetLoanDetails(int applicationId)
{
return HandleResponseException(response =>
{
var apiClient = fluentApiClient.NewGetRequest("investnow", "get-loan-detail").AddQuery("application_id", $"{applicationId}");
var resp = apiClient.GetResponse();
response.StatusCode = resp.Success && resp.Data != null ? HttpStatusCode.OK : HttpStatusCode.BadRequest;
response.Content = new ObjectContent<ApiResponse>(resp, GetMediaTypeFormatter());
});
}
}
}
当我从 Postman 调用端点时,它有效。
但是当我尝试从 JavaScript 调用相同的端点时,我每次都会收到 404 错误。我正在处理遗留应用程序,之前我已经编写过这样的端点,但我不明白为什么它不起作用。请注意,这仅发生在需要参数的端点上。只要没有传递路由参数,同一控制器中的其他端点就可以正常工作。即使 POST 请求工作 fine.Just 带参数的 GET 请求也是问题。
在我的 Javascript 代码中,我用 Axios 调用 enpoints
getLoanDetails: function () {
var self = this;
$.LoadingOverlay("show");
axios.get('/customer/api/abl/get-loan-detail', {
params: {
application_id: 50
}
})
.then(function (response) {
})
.catch(function (error) {
})
.then(function () {
});
},
getLoanHistory2: function () {
var self = this;
axios.get('/customer/api/abl/my-loans', {
params: {
customer_id: self.customerId
}
})
.then(function (response) {
})
.catch(function (error) {
})
.then(function () {
});
}
axios-get's params 添加参数作为查询字符串
所以axios.get('/user?ID=12345')
等同于
axios.get('/user', {
params: {
ID: 12345
}
})
您的代码已将参数定义为路由而不是查询参数,因此
axios.get('/customer/api/abl/get-loan-detail', {
params: {
application_id: 50
}
})
正在解析为 /customer/api/abl/get-loan-detail?application_id=50
而不是 /customer/api/abl/get-loan-detail/50
这就是你得到 404 的原因。
您可以按如下方式创建 url
axios.get(`/customer/api/abl/my-loans/${self.customerId}`).then