Reputation: 183
Using my Post functions to create rows in tables generates a 500 response AND writes the data. The response body is a html, and the error seems to be the following:
<h1>An unhandled exception occurred while processing the request.</h1>
<div class="titleerror">InvalidOperationException: No route matches the supplied values.</div>
<p class="location">Microsoft.AspNetCore.Mvc.CreatedAtRouteResult.OnFormatting(ActionContext context)</p>
Apparently, my error occurs at my CreatedAtRoute
response.
I'm having a hard time understanding exactly what post does and how links should get generated for the new entries in tables.
I am using swagger to call my endpoints.
I tried reading up on how to generate links, but the concepts drown me.
I tried messing around with the CreatedAtRoute
response but I can't seem to figure out the proper parameters I need to send, or what exactly it does.
Controller:
[ProducesResponseType(typeof(Location),200)]
[HttpGet("locations/{locationId}", Name = "LocationById")]
public IActionResult GetLocation(int locationId)
[HttpPost("locations")]
public IActionResult CreateLocation([FromBody] CreateLocationRequest createLocationRequest)
{
try
{
if (createLocationRequest == null)
{
return NotFound();
}
return CreatedAtRoute("LocationById",_repository.Location.CreateLocation(createLocationRequest),createLocationRequest);
}
catch (Exception e)
{
return StatusCode(500, e.InnerException);
}
}
Repository:
public int CreateCompany(string name, int countryId, int? parentId)//string name, int countryId, int? parentId
{
Company company = new Company()
{
CountryId = countryId,
Name = name,
ParentId = parentId
};
_context.Companies.Add(company);
_context.SaveChanges();
return company.Id;
}
Startup route config:
app.UseHttpsRedirection();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
I need a proper route towards my newly created table entries. Any suggestions on how to obtain this would be extremely appreciated.
After reading a bit about CreatedAtRoute, I tried tweaking my functions to try and see if passing the entity itself instead of the model when calling CreatedAtRoute
would do anything to help my cause. It did not.
New methods: Repository:
public LocationReturns CreateLocation(CreateLocationRequest createLocationRequest)
{
if (_context.Companies.FirstOrDefault(c => c.Id == createLocationRequest.CompanyId) != null)
{
LocationReturns locationReturns=new LocationReturns();
Location location = new Location()
{
Address = createLocationRequest.Address,
AllowedAsSource = createLocationRequest.AllowedAsSource,
City = createLocationRequest.City,
CompanyId = createLocationRequest.CompanyId,
Name = createLocationRequest.Name,
Picture = createLocationRequest.Picture,
PostCode = createLocationRequest.Postcode,
Region = createLocationRequest.Region
};
_context.Locations.Add(location);
_context.SaveChanges();
locationReturns.id = location.Id;
locationReturns.loc = location;
return locationReturns;
}
LocationReturns loc = new LocationReturns();
return loc;
}
Controller:
[HttpPost("locations")]
public IActionResult CreateLocation([FromBody] CreateLocationRequest createLocationRequest)
{
try
{
if (createLocationRequest == null)
{
return NotFound();
}
LocationRepository.LocationReturns locationReturns=new LocationRepository.LocationReturns();
locationReturns = _repository.Location.CreateLocation(createLocationRequest);
return CreatedAtRoute("DefaultApi",new {id=locationReturns.id}, locationReturns.loc);
}
catch (Exception e)
{
return StatusCode(500, e.InnerException);
}
}
Where LocationReturns is a struct.
Upvotes: 0
Views: 388
Reputation: 183
The anonymous object generated inside the CreateAtRoute()
had to have an int prop with the same name as the parameter declared in the route. Ex:
[HttpGet("/locations/{locationId}, Name="LocationCreated"
requires
CreatedAtRoute("LocationCreated", new {locationId=id}, locationObject);
instead of CreatedAtRoute("LocationCreated", new {id=_id}, locationObject);
Upvotes: 1