I have two ASP.NET Core Web API projects called app.account
and core.account
. I the both projects have some testing API endpoint like this.
In app.account
:
namespace app.account.Controllers
{
[ApiController]
public class SubInteractiveStatementController : ControllerBase
{
[HttpPost]
[Route("account/istatement")]
public async Task<ActionResult> Test(Test cif)
{
return Ok("hello gamma");
}
}
}
public class Test
{
public string cif { get; set; }
}
In core.account
:
namespace core.account.Controllers
{
[ApiController]
public class CasaDetailController : ControllerBase
{
[HttpPost]
[Route("account/istatement")]
public async Task<ActionResult> Test(Test cif)
{
return Ok("hello gamma");
}
}
}
I’m calling the above API endpoint throughout the client side like this:
try {
CommonResponse commonResponse = new();
var data_ = JsonConvert.SerializeObject(test);
var buffer_ = System.Text.Encoding.UTF8.GetBytes(data_);
var byteContent_ = new ByteArrayContent(buffer_);
byteContent_.Headers.ContentType = new MediaTypeHeaderValue("application/json");
//string _urls = "http://localhost:5088/account/istatement";
string _urls = "http://localhost:5035/account/istatement";
var responses_ = await _httpClient.PostAsync(_urls, byteContent_);
if (responses_.StatusCode == HttpStatusCode.OK) {
Console.WriteLine("[GetPrimeryAccount] Response: Success");
string body = await responses_.Content.ReadAsStringAsync();
var rtns = JsonConvert.DeserializeObject < CommonResponse > (body);
}
} catch (global::System.Exception) {
throw;
}
http://localhost:5088/account/istatement is the app.account API endpoint and http://localhost:5035/account/istatement is the core.account API endpoint.
When I call the app.account
, I’m getting the following error.
{ StatusCode: 400, ReasonPhrase: ‘Bad Request’, Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
Date: Wed, 19 Oct 2022 08:43:21 GMT
Server: Kestrel
Transfer-Encoding: chunked
}}
But it’s properly calling the core.account
API endpoint.
Both URL’s are correct, I checked both endpoints using Postman, and that worked properly.
What could be the reason for this? Could it be a HTTP version issue? How can I determine the exact reason for this? Please help. Thanks
3
Answers
Regarding the request
Instead of manually composing the data as byte array content, you can use the StringContent class
If you want to shorten it even more, there’s a extension
Well, you are certainly getting the error because of your
[Route("account/istatement")]
route. You cannot usemultiple route attribute
for same action, not matter even if the controller are different even if the projcts are different within the same solution.Remember your
route attribute
should beunique
within the solution if you defined it following this fashion[Route("account/istatement")]
.Solution:
If you comment either of this
route attribute
from either of the controller it would work. Or you can use theroute attribute
globally on your controller level.Output:
The problem seems to be in a part that you have not shared with us.
Please share both of the API project’s
startup.cs
here. For a hint, If you are using any custom header validation middleware, make sure to pass those header values properly. Otherwise, that may cause to occur 400 Bad Request.Compare the both
startp.cs
files and you will be able to identify the problem there.