Hi, sorry for the late answer.
could you please tell us when you get this error? On loading or while sending any request?
I received this error message when I try to test POST method via swagger
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi,
I've application where I try to make one-to-many relationship. the controller was created (see the post here : ef-one-to-many-to-assure-the-link-with-the-main-ta.html)
I've the DeveloperActionsController that I tested with swagger. So I received the error message.
Have you an idea how I can fix this error?
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-938ace42dd83c35b05f556b91c0a3170-c2901182370015ff-00",
"errors": {
"developer": [
"The developer field is required."
],
"$.actionItems[0].developer": [
"The JSON value could not be converted to AppTest.Shared.Models.Developer. Path: $.actionItemsTest[0].developer | LineNumber: 12 | BytePositionInLine: 31."
]
}
}
Class model :
public partial class Developer
{
[Key]
public int DeveloperId { get; set; }
public List<ActionItem>? ActionItems{ get; set; }
}
public class ActionItem
{
[Key]
public int ActionItemId { get; set; }
public string? Tilte { get; set; }
public string? Description { get; set; }
public string? State { get; set; }
public DateTime? OpenDate { get; set; }
public DateTime? PlanDate { get; set; }
public DateTime? CloseDate { get; set; }
public int DeveloperId { get; set; }
public Developer? Developer { get; set; }
}
Controller
[Route("api/[controller]")]
[ApiController]
public class DeveloperActionsController : ControllerBase
{
private readonly ApplicationDBContext _contexts;
public DeveloperActionsController(ApplicationDBContext contexts)
{
this._contexts= contexts;
}
// GET: api/<DeveloperActionController>
[HttpGet]
public IEnumerable<Developert> GetDevelopersWithActions()
{
return _contexts.DeveloperTests.Include(a => a.ActionItems).ToList();
}
// GET api/<DeveloperActionController>/5
[HttpGet("{id}")]
public async Task<ActionResult<Developer>> GetDeveloperActions(int id)
{
DeveloperTest? developer = await _contexts.Developer
.Include(a => a.ActionItems)
.FirstOrDefaultAsync(d => d.DeveloperId == id);
if (developer == null)
{
return NotFound();
}
return Ok(developer);
}
// POST api/<DeveloperActionController>
[HttpPost]
public async Task<ActionResult<Developer>> PostAsync([FromBody] Developer developer)
{
_contexts.Developer.Add(developer);
await _contexts.SaveChangesAsync();
return CreatedAtAction(nameof(GetDeveloperActions), new { id = developer.DeveloperId }, developer);
}
// PUT api/<DeveloperActionController>/5
[HttpPut("{id}")]
public async Task<ActionResult<Developer>> Put(int id, [FromBody] ActionItem actionItem)
{
DeveloperTest? developer = await _contexts.Developer
.Include(a => a.ActionItems)
.FirstOrDefaultAsync(d => d.DeveloperId == id);
if (developer == null)
{
return NotFound();
}
developer.ActionItems.Add(actionItem);
await _contexts.SaveChangesAsync();
return CreatedAtAction(nameof(GetDeveloperActions), new { id = developer.DeveloperId }, developer);
}
// DELETE api/<DeveloperActionController>/5
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(int id)
{
DeveloperTest? developer = await _contexts.Developer
.Include(a => a.ActionItems)
.FirstOrDefaultAsync(d => d.DeveloperId == id);
if (developer == null)
{
return NotFound();
}
_contexts.Remove(developer);
await _contexts.SaveChangesAsync();
return NoContent();
}
}
Hi, sorry for the late answer.
could you please tell us when you get this error? On loading or while sending any request?
I received this error message when I try to test POST method via swagger
My application is relatively heavy for testing.
So I redid a test application and I have exactly what you suggested to put a foreign key.
The controller, modelclass and ApplicationDBcontext are what I have at the beginning of this post. I also did a migration to get access to the Developers & ActionItems tables.
I added a razor page to initialize the Developers table first. Nothing happens when I do htpp request.
Plus When I added the simple for loop I the error.
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Object reference not set to an instance of an object.
System.NullReferenceException: Object reference not set to an instance of an object.
Anyway when I use swagger and test Get, Post, Put, Delete I receive the status 404!!
I would really like to understand why I get this type of error when I am doing something that is actually quite simple.
@page "/test"
@using AppTest.Shared.Models
@using System.Text.Json
@using System.ComponentModel
@inject HttpClient Http
<h3>TestDev</h3>
<table class="table">
<thead>
<tr>
<th>ID</th>
</tr>
</thead>
<tbody>
@foreach (var dev in developer)
{
<tr>
<td>@dev.DeveloperId</td>
</tr>
}
</tbody>
</table>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>State</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
@code{
Developer[]? developer { get; set; }
protected override async Task OnInitializedAsync()
{
developer=await Http.GetFromJsonAsync<Developer[]>("api/developeractions");
}
}
your issues are from using entity models as api models. in the ActionItem you are trying to post navigation values. create separate POCO request/response models for the json.
Hi,
I tried it in controller :
return Ok(JsonSerializer.Serialize(developer, new JsonSerializerOptions
{
PropertyNamingPolicy = null
}));
your issues are from using entity models as api models
Could you explain because I don't really understand? thanks in advance
I have searched the internet and I can't find (or don't quite understand the principle) a clear explanation for creating separate POCO request/response models for the json.
Could you please help me to unblock?
thanks in advance !!