Axios Post -没有控制台错误但不工作



Axios post不工作,击中API控制器但没有击中相应的方法。控制台也没有任何错误

Get功能正常。

UI代码:

const register =async(companyDetails)=>{
// const ApiURL = await environmentConfig.getBaseURL();
const ApiURL = "https://localhost:44313/api/v1.0";
try{
const result = await axios(`${ApiURL}/market/company/register`,{
method:'POST',
headers :{
'Accept':'application/json',
'Content-Type':'application/json'
},
body :JSON.stringify(companyDetails),

});
return result.json();
}
catch(err){
return err;
}

}

<API代码/strong>

[Route("api/v1.0/market")]
[ApiController]
public class StockController 
{
private readonly MongodbService _mongodbService;
public StockController(MongodbService mongodbService)
{
_mongodbService = mongodbService;
}

[HttpPost]
[Route("company/register")]
public async Task<CompanyDetails> Register([FromBody] CompanyDetails companyDetails)
{
await _mongodbService.Register(companyDetails);
return companyDetails;
}
}

创建一个控制器但是你忘了指定的方法处理该请求。关于如何在c#中编程控制器的示例可以在这里找到:

[Route("api/[controller]")]
[ApiController]
public class EmployeesController : ControllerBase
{
private readonly IEmployeeRepository employeeRepository;
public EmployeesController(IEmployeeRepository employeeRepository)
{
this.employeeRepository = employeeRepository;
}
[HttpPost]
public async Task<ActionResult<Employee>> CreateEmployee(Employee employee)
{
try
{
if (employee == null)
return BadRequest();
var createdEmployee = await employeeRepository.AddEmployee(employee);
return CreatedAtAction(nameof(GetEmployee),
new { id = createdEmployee.EmployeeId }, createdEmployee);
}
catch (Exception)
{
return StatusCode(StatusCodes.Status500InternalServerError,
"Error creating new employee record");
}
}
}

我从这一页取了这个例子。

相关内容