我已经在 ASP.NET 中编写了一个 Web API,并且在调用该 Web API 的桌面应用程序中编写了一个测试方法。该测试方法插入一条新记录,更新该记录,然后删除该记录。插入成功,可以在数据库表中看到新记录。删除方法也有效,记录会从表中删除。更新方法失败并显示以下错误消息:
响应状态代码不表示成功:405(方法不允许)。
我使用 Visual Studio 的正常流程创建了 Web API,并且没有修改它构建的框架代码。我创建了一个名为 WebSave 的实用程序函数(如下所示)来处理插入和更新函数。我不明白为什么更新在本质上有什么不同。希望有人能告诉我。
private void SaveData_Test()
{
string sRequestURI = "http://localhost:49625/api/tipshare_def";
int iStoreNo = 40004;
tipshare_def oTipShareDef = new tipshare_def() { storeno = iStoreNo, tipshare_type = 1, total_bar_share = (decimal)1.0, food_bar_share = (decimal)0.6, alc_bar_share = (decimal)0.4, job_exclude1 = 1, job_exclude2 = 1, job_exclude3 = 0 };
Utilities.WebSave(sRequestURI, oTipShareDef, typeof(tipshare_def), true);
oTipShareDef.food_bar_share = (decimal)0.7;
oTipShareDef.alc_bar_share = (decimal)0.3;
Utilities.WebSave(sRequestURI, oTipShareDef, typeof(tipshare_def), false);
string sRequestURI2 = "http://localhost:49625/api/tipshare_def/" + iStoreNo.ToString("0");
Utilities.WebDelete(sRequestURI2);
}
public static async Task WebSave(string sRequestURI, object oRecord, Type tRecordType, bool bNewRecord)
{
try
{
HttpClient oHttpClient = new HttpClient();
if (bNewRecord == true)
{
HttpResponseMessage oResponse = await oHttpClient.PostAsJsonAsync(sRequestURI, oRecord);
oResponse.EnsureSuccessStatusCode();
HttpStatusCode oStatus = oResponse.StatusCode;
string sStatus = oStatus.ToString();
}
else
{
HttpResponseMessage oResponse = await oHttpClient.PutAsJsonAsync(sRequestURI, oRecord);
oResponse.EnsureSuccessStatusCode();
HttpStatusCode oStatus = oResponse.StatusCode;
string sStatus = oStatus.ToString();
}
}
catch (Exception oException)
{
HandleError("WebSave", oException);
}
}
//这是相关的Web API代码:
// PUT: api/tipshare_def/5
[ResponseType(typeof(void))]
public async Task<IHttpActionResult> Puttipshare_def(int id, tipshare_def tipshare_def)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != tipshare_def.storeno)
{
return BadRequest();
}
db.Entry(tipshare_def).State = EntityState.Modified;
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!tipshare_defExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST: api/tipshare_def
[ResponseType(typeof(tipshare_def))]
public async Task<IHttpActionResult> Posttipshare_def(tipshare_def tipshare_def)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.tipshare_def.Add(tipshare_def);
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (tipshare_defExists(tipshare_def.storeno))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtRoute("DefaultApi", new { id = tipshare_def.storeno }, tipshare_def);
}
最佳答案
在这种情况下,您的参数不匹配。您的 PUT 请求中有一个 Id,但 POST 中没有。
作为一般规则,如果您仅收到针对少数动词(通常是 PUT 和 DELETE)的 405 响应,则需要使用以下更改之一来更新您的 web.config:
<system.webServer>
<handlers>
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*."
verb="GET,HEAD,POST,DEBUG,PUT,DELETE"
type="System.Web.Handlers.TransferRequestHandler"
preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
WEBDAV 也有可能干扰您的请求。以下将删除它。
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule"/>
</modules>
关于c# - 为什么我的 Web API 仅在 Put 时失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29971011/