场景
嗨,我有用ASP.Net core 2.2编写的命令API控制器。 Controller继承自 ControllerBase ,并具有属性 ApiController 。我想添加Location标头。我也查询了odata控制器。 Odata版本:7.2.2 。
Odata代码:
我的Odata控制器:
[ODataRoutePrefix("categories")]
public class ODataCategoriesController : ODataController
Odata采取行动:
[EnableQuery]
[ODataRoute("{id}")]
public async Task<actionResult<Category>> Getasync(Guid id)
启动
opt.EnableEndpointRouting = false;
...
app.UseHttpsRedirection();
app.UseMvc(options =>
{
options.EnableDependencyInjection();
options.Select().Filter().OrderBy().Count().Expand().MaxTop(100).SkipToken();
options.MapodataServiceRoute("odata","api/odata",GetExplicitEdmModel());
});
尝试
我尝试使用CreatedAtaction,但是收到: InvalidOperationException:没有路由与提供的值匹配。
在我的POST控制器中:
return CreatedAtaction("Get","ODataCategories",new { id= categoryResponse.Id },categoryResponse);
尝试2次
我也尝试过手动返回Location标头。但是我收到了: 标题不存在。
代码
[HttpPost]
[ProducesResponseType((int)HttpStatusCode.Created)]
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
public async Task<actionResult<CreateCategoryResponse>> PostCategory(
[FromBody]CreateCategoryCommand createCategoryCommand)
{
CreateCategoryResponse categoryResponse =
await _mediator.Send(createCategoryCommand);
if (categoryResponse == null)
{
return BadRequest();
}
HttpContext.Response.Headers["Location"] =
$"SomeBuiltLocation";
return Created("",categoryResponse);
}
摘要
我正在寻找能够使我包含Location标头的解决方案。不管是使用CreatedAt还是手动生成。