運行時,我知道這是一個常見的解決問題,我所做的一切,這裏的許多職位,建議。當我嘗試從在本地IIS下運行的MVC5前端中使用WebAPI(版本2)刪除記錄時,出現404 Not Found響應。下面是我試過的東西:刪除/ PUT動詞導致404的WebAPI沒有找到,只能在本地
我已在我的WebAPI的web.config <system.webServer />
下以下幾點:
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
</modules>
<handlers>
<remove name="WebDAV" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
我在跟着指示:http://geekswithblogs.net/michelotti/archive/2011/05/28/resolve-404-in-iis-express-for-put-and-delete-verbs.aspx,基本上說來修改IIS中的「Handler Mappings」中的ExtensionlessUrlHandler-Integrated-4.0
。它說雙擊處理程序,單擊「請求限制」和「允許PUT和刪除動詞」。我已經完成了這個工作,但仍然遇到了404錯誤。
我已經完成了IIS重置。
這裏是我調用WebAPI刪除方法的MVC5前端代碼 - 請注意,當我手動導航到api/bulletinboard/get/{0}
,其中{0}
是一個整數時,我得到一個有效的JSON
響應。下面,contactUri
是http://localhost/SiteName/api/bulletinboard/get/53
返回有效JSON
:
[HttpPost, ActionName("Delete")]
public ActionResult Delete(string appId, int id)
{
response = client.GetAsync(string.Format("api/bulletinboard/get/{0}", id)).Result;
contactUri = response.RequestMessage.RequestUri;
response = client.DeleteAsync(contactUri).Result;
if (response.IsSuccessStatusCode)
{
return RedirectToAction("MessageList", new { appId = appId });
}
else
{
LoggerHelper.GetLogger().InsertError(new Exception(string.Format(
"Cannot delete message due to HTTP Response Status Code not being successful: {0}", response.StatusCode)));
return View("Problem");
}
}
,這裏是我的WebAPI刪除方法:
[HttpDelete]
public HttpResponseMessage Delete(int id)
{
BulletinBoard bulletinBoard = db.BulletinBoards.Find(id);
if (bulletinBoard == null)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
db.BulletinBoards.Remove(bulletinBoard);
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException ex)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
}
return Request.CreateResponse(HttpStatusCode.OK, bulletinBoard);
}
這是我在我的WebAPI項目WebApiConfig.cs:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
config.EnableCors();
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "ApiWithActionName",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
config.Formatters.Add(new PlainTextFormatter());
}
問題 :我還有什麼可以嘗試解決這個錯誤?從我的本地環境部署到我公司的開發服務器時,這種方式很好。
您的請求網址是'api/bulletinboard/get/{0}',而您試圖達到刪除操作...不應該是'api/bulletinboard/delete/{0}'? –
@KiranChalla不,這應該是我的方式。 'DeleteAsync'獲取GET請求的結果並刪除它。我在另一個項目中按原樣工作。不過,我改變了你的建議,只是爲了看看會發生什麼,而且我有同樣的404錯誤。我認爲有一些關於IIS不喜歡的DELETE請求... –
@KiranChalla啊,這是一個非常奇怪的情況...看看我的答案在下面。 –