我正在设置API以与数据库交互,并且当api收到帖子以创建已经存在于其相应表格中的条目时我想要返回代码209与解释性消息。所以我尝试了这个:
控制器
public async Task<IHttpActionResult> PostOrganizationAsync(Organization organization)
{
using (var context = new ContextHandler())
{
bool added = await context.AddOrganizationAsync(organization);
if (added)
{
return Ok();
}
else
{
using (var httpContent = new StringContent("An organization with this Id already exists on the database"))
{
return new CustomHttpResponse(HttpStatusCode.Conflict, httpContent);
}
}
}
}
ContextHandler中
class ContextHandler : IDisposable
{
static AuditLogsEntities LogDatabaseContext;
public ContextHandler()
{
LogDatabaseContext = new AuditLogsEntities();
LogDatabaseContext.Configuration.LazyLoadingEnabled = false;
}
public void Dispose()
{
LogDatabaseContext.Dispose();
}
public async Task<bool> AddOrganizationAsync(Organization organization)
{
var findOrg = await LogDatabaseContext.OrganizationSet.FindAsync(organization.Id);
if (findOrg != null)
{
return false;
}
else
{
LogDatabaseContext.OrganizationSet.Add(organization);
await LogDatabaseContext.SaveChangesAsync();
return true;
}
}
}
CustomHttpResponse
public class CustomHttpResponse : IHttpActionResult
{
public HttpStatusCode StatusCode;
public HttpContent Content;
public CustomHttpResponse(HttpStatusCode httpStatusCode, HttpContent httpContent)
{
StatusCode = httpStatusCode;
Content = httpContent;
}
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
public async Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
{
HttpResponseMessage httpResponse = null;
using (httpResponse = new HttpResponseMessage(StatusCode))
{
httpResponse.Content = Content;
return httpResponse;
}
}
}
发布一个新的有效条目可以正常工作,但尝试发布一个已存在的条目来测试209响应会产生一个ObjectDisposedException
,我认为这是因为我的using
块之一而发生的,但是问题是我不确定它是哪个块以及它为什么会导致错误。
private HttpRequestMessage _request
内设置HttpResponseMessage
属性时抛出异常
答案 0 :(得分:3)
不要使用块。只需将它们存储在一个私有变量中,并通过控制器处理它们进行处理。此外,您可以将Task.FromResult用于已完成的任务。
<强>控制器强>
private StringContent _httpContent
private CustomHttpResponse _customHttpResponse
public async Task<IHttpActionResult> PostOrganizationAsync(Organization organization)
{
using (var context = new ContextHandler())
{
bool added = await context.AddOrganizationAsync(organization);
if (added)
{
return Ok();
}
else
{
this._httpContent = new StringContent("An organization with this Id already exists on the database"))
this._customHttpResponse = new CustomHttpResponse(HttpStatusCode.Conflict, httpContent);
return this._customHttpResponse;
}
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (this._httpContent != null)
{
this._httpContent.Dispose();
}
if (this._customHttpResponse != null)
{
this._customHttpResponse.Dispose();
}
}
}
<强> CustomHttpResponse 强>
public class CustomHttpResponse : IHttpActionResult
{
public HttpStatusCode StatusCode;
public HttpContent Content;
public CustomHttpResponse(HttpStatusCode httpStatusCode, HttpContent httpContent)
{
StatusCode = httpStatusCode;
Content = httpContent;
}
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
HttpResponseMessage httpResponse = new HttpResponseMessage(StatusCode);
httpResponse.Content = Content;
return Task.FromResult(httpResponse);
}
}