使用我的Post函数在表中创建行会生成500响应并写入数据。 响应正文是html,错误似乎如下:
<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>
显然,我的错误发生在我的CreatedAtRoute
响应中。
我很难确切地了解帖子的内容以及如何为表中的新条目生成链接。
我正在用招摇叫叫端点。
我尝试阅读有关如何生成链接的内容,但是这些概念淹没了我。
我尝试弄乱CreatedAtRoute
响应,但似乎无法弄清楚需要发送的参数或参数的正确性。
控制器:
[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);
}
}
存储库:
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;
}
启动路由配置:
app.UseHttpsRedirection();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
我需要一条通往我新创建的表条目的正确路径。任何有关如何获得此建议的建议将不胜感激。
在阅读了有关CreatedAtRoute的一些知识之后,我尝试调整函数以尝试查看在调用CreatedAtRoute
时传递实体本身而不是模型是否对我的事业有帮助。没有。
新方法: 储存库:
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;
}
控制器:
[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);
}
}
LocationReturns是结构。
答案 0 :(得分:0)
在CreateAtRoute()
内部生成的匿名对象必须具有一个与该路线中声明的参数同名的int属性。例如:
[HttpGet("/locations/{locationId}, Name="LocationCreated"
要求
CreatedAtRoute("LocationCreated", new {locationId=id}, locationObject);
而不是CreatedAtRoute("LocationCreated", new {id=_id}, locationObject);