Http 500 Internal Server Error on POST Request - http

I'm trying to send data to server with POST method but I got 500 Internal Server Error. Here's my codes. What do I need to fix this? Thank you.
Rest API POST Method
public void Post([FromBody]OrderLineTerminalViewModel[] OrderLineItems)
{
foreach (var OrderLineItem in OrderLineItems)
{
…………
}
_context.SaveChanges();
}
MODEL
public class OrderLineTerminalViewModel
{
public int Id { get; set; }
public string Unit { get; set; }
public decimal Qtty { get; set; }
public int ReferanceNumberId { get; set; }
}
SAMPLE JSON
[ {'Id':73039,'Unit':'PCS','Qtty':33.0,'ReferanceNumberId':20041},
{'Id':73040,'Unit':'PC','Qtty':120.0,'ReferanceNumberId':20041}]

Please check my API which is working completely fine(Tested).
Note:-
Add HttpPost before API method and remove [FromBody].
[RoutePrefix("api")]
public class ValuesController : ApiController
{
[HttpPost]
[Route("postrequest")]
public void Post(OrderLineTerminalViewModel[] OrderLineItems)
{
//your Code
}
}
Model:
public class OrderLineTerminalViewModel
{
public int Id { get; set; }
public string Unit { get; set; }
public decimal Qtty { get; set; }
public int ReferanceNumberId { get; set; }
}
Json:-
[ {"Id":73039,"Unit":"PCS","Qtty":33.0,"ReferanceNumberId":20041},
{"Id":73040,"Unit":"PC","Qtty":120.0,"ReferanceNumberId":20041}]
Tested

Related

.NET web API one to many relation

I'm building a one to many relation web API with .NET and got error 400.
I have two tables:
public class CarBrand
{
public int CarBrandId { get; set; }
public string Name { get; set; }
[JsonIgnore]
public List<CarModel> CarModels { get; set; } = new List<CarModel>();
}
public class CarModel
{
public int CarModelId { get; set; }
public string ModelName { get; set; }
public int CarBrandId { get; set; }
[JsonIgnore]
public CarBrand CarBrand { get; set; } = new CarBrand();
}
My Controller is:
[HttpPost]
public async Task<ActionResult<CarModel>> PostCarModel(CarModel carModel)
{
_context.CarModels.Add(carModel);
await _context.SaveChangesAsync();
return CreatedAtAction("GetCarModel", new { id = carModel.CarModelId }, carModel);
}
But when I make a POST request and try to add CarModel, I get error 400:
CarBrand:[
"This Field is required!"
]
Thanks!

Refit me showing an error when initializing

I am trying to consume a service using Refit and I have the following problem. I was looking at the documentation but I can't find a solution and I don't know if I should do anything else.
This is my model
public partial class Global
{
[JsonProperty("updated")]
public long Updated { get; set; }
[JsonProperty("country")]
public string Country { get; set; }
[JsonProperty("countryInfo")]
public List<CountryInfo> CountryInfo { get; set; }
}
My second class:
public partial class CountryInfo
{
[JsonProperty("_id")]
public long? Id { get; set; }
[JsonProperty("iso2")]
public string Iso2 { get; set; }
[JsonProperty("iso3")]
public string Iso3 { get; set; }
[JsonProperty("lat")]
public double Lat { get; set; }
[JsonProperty("long")]
public double Long { get; set; }
[JsonProperty("flag")]
public Uri Flag { get; set; }
}
Application with Refit
Link : https://corona.lmao.ninja/v2/countries
[Get("/v2/countries")]
Task<CountryInfo> GetCountry();
In my viewModel
private async Task GetTaskInterf()
{
var webApp = RestService.For<ICovidInterface>("https://corona.lmao.ninja");
var result = await webApp.GetGlobalCountry();
}
At the time of initialization

Passing Date in an API

I am working on .Net Core and want to post attributes in the api containing dates but I m not able to handle dates for an API. How to overcome with this error.
using System;
using System.Collections.Generic;
namespace Server.Dtos
{
public class ProjectSessionDto
{
public int Id { get; set; }
public string Activity { get; set; }
public string ResourcePerson { get; set; }
public DateTime? TentativeDate { get; set; }
public DateTime SubmissionDate { get; set; }
public int SemesterId { get; set; }
public string Program { get; set; }
public string Batch { get; set; }
}
}
Controller Code...
[HttpPost("addprojectsession")] //Since there would be 2 Post Methods. Login and Register
public async Task<IActionResult> AddProjectSession(ProjectSessionDto projectsession)
{
var semester = await _semester.GetSemesterWithProjectSession(projectsession.SemesterId);
semester.projectsessions.Add(_mapper.Map<ProjectSessionDto,ProjectSession>(projectsession));
await _semester.AddSemesterWithProject(semester);
return Ok();
}
}
There are two issues you need to fix :
In Postman ,change the values with double quotes:
"tentativeDate": "12/12/2019",
"SubmissionDate":"12/12/2019"
In controller , use [FromBody]to make asp.net core model binding wokring for reading value from request body :
public async Task<IActionResult> AddProjectSession([FromBody]ProjectSessionDto projectsession)

ASP.NET Core Web Api post request 404 Not Found

I have post method in the controller:
[Route("api/[controller]")]
[ApiController]
public class CompanyController : ControllerBase
{
private readonly ICompanyService _companyService;
public CompanyController(ICompanyService companyService)
{
_companyService = companyService;
}
[HttpPost]
[Route("saveParameters")]
public StatusCodeResult SaveCompanyValuationParameters([FromBody] CompanyValuationParametersDTO cvpDto)
{
var userName = Guid.NewGuid().ToString();
var cvpDto1 = new CompanyValuationParametersDTO(27, 5, 4,
5, 5, 5, userName);
_companyService.SaveCompanyValuationParameters(cvpDto1);
return StatusCode(201);
//return CreatedAtAction("SaveCompanyValuationParameters", new { id = cvpDto1.Id }, cvpDto1);
}
}
the body of the method is a complete mess now but I just want to make it work. I've tried several routes and return types like StatusCodeResult, ActionResult, void but still I can't reach this method from postman:
So how to reach the post method from postman? I think I am missing something simple but still can't figure out what exactly it is.
DTO:
public class CompanyValuationParametersDTO
{
public int Id { get; set; }
public int Salary { get; set; }
public int ProfessionalGrowth { get; set; }
public int CompanyBenefits { get; set; }
public int CommunicationWithColleagues { get; set; }
public int EducationSector { get; set; }
public string UserName { get; set; }
}

Using ComplexType with ToList causes InvalidOperationException

I have this model
namespace ProjectTimer.Models
{
public class TimerContext : DbContext
{
public TimerContext()
: base("DefaultConnection")
{
}
public DbSet<Project> Projects { get; set; }
public DbSet<ProjectTimeSpan> TimeSpans { get; set; }
}
public class DomainBase
{
[Key]
public int Id { get; set; }
}
public class Project : DomainBase
{
public UserProfile User { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public IList<ProjectTimeSpan> TimeSpans { get; set; }
}
[ComplexType]
public class ProjectTimeSpan
{
public DateTime TimeStart { get; set; }
public DateTime TimeEnd { get; set; }
public bool Active { get; set; }
}
}
When I try to use this action I get the exception The type 'ProjectTimer.Models.ProjectTimeSpan' has already been configured as an entity type. It cannot be reconfigured as a complex type.
public ActionResult Index()
{
using (var db = new TimerContext())
{
return View(db.Projects.ToList);
}
}
The view is using the model #model IList<ProjectTimer.Models.Project>
Can any one shine some light as to why this would be happening?
Your IList<ProjectTimeSpan> property is not supported by EF. A complex type must always be part of another entity type, you cannot use a complex type by itself. If you absolutely need to have ProjectTimeSpan as a complex type, you will need to create a dummy entity type that only contains a key and a ProjectTimeSpan, and change the type of Project.TimeSpans to a list of that new type.

Resources