CreatedAtRoute 生成异常:"No route matches the supplied values."
CreatedAtRoute generates an exception : "No route matches the supplied values."
使用我的 Post 函数在 table 中创建行生成 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
响应中。
我很难理解 post 的作用以及如何为 table 中的新条目生成链接。
我正在使用 swagger 来调用我的端点。
我尝试阅读有关如何生成链接的内容,但这些概念淹没了我。
我试着弄乱 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?}");
});
我需要一条通向新创建的 table 条目的正确路径。非常感谢任何关于如何获得它的建议。
在阅读了一些有关 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 是一个结构。
在 CreateAtRoute()
中生成的匿名对象必须有一个与路由中声明的参数同名的 int prop。前任:
[HttpGet("/locations/{locationId}, Name="LocationCreated"
需要
CreatedAtRoute("LocationCreated", new {locationId=id}, locationObject);
而不是 CreatedAtRoute("LocationCreated", new {id=_id}, locationObject);
使用我的 Post 函数在 table 中创建行生成 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
响应中。
我很难理解 post 的作用以及如何为 table 中的新条目生成链接。
我正在使用 swagger 来调用我的端点。
我尝试阅读有关如何生成链接的内容,但这些概念淹没了我。
我试着弄乱 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?}");
});
我需要一条通向新创建的 table 条目的正确路径。非常感谢任何关于如何获得它的建议。
在阅读了一些有关 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 是一个结构。
在 CreateAtRoute()
中生成的匿名对象必须有一个与路由中声明的参数同名的 int prop。前任:
[HttpGet("/locations/{locationId}, Name="LocationCreated"
需要
CreatedAtRoute("LocationCreated", new {locationId=id}, locationObject);
而不是 CreatedAtRoute("LocationCreated", new {id=_id}, locationObject);