【发布时间】:2014-01-15 14:56:16
【问题描述】:
我知道这是一个经常解决的问题,并且我已经完成了许多关于 SO 的帖子所建议的一切。当我尝试使用在本地 IIS 下运行的 MVC5 前端使用 WebAPI(版本 2)删除记录时,我收到 404 Not Found 响应。以下是我尝试过的事情:
我在我的 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“处理程序映射”中的 ExtensionlessUrlHandler-Integrated-4.0。它说要双击处理程序,单击“请求限制”和“允许 PUT 和 DELETE 动词”。我已经这样做了,但我仍然收到 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());
}
问题:我还有什么办法可以解决这个错误?当从我的本地环境部署到我公司的开发服务器时,这可以正常工作。
【问题讨论】:
-
您的请求 url 是
api/bulletinboard/get/{0},而您正在尝试执行删除操作...不应该是api/bulletinboard/delete/{0}? -
@KiranChalla 不,我应该是这样的。
DeleteAsync获取 GET 请求的结果并将其删除。我有这个在另一个项目中工作。但是,我确实将其更改为您建议的内容,只是为了看看会发生什么,并且我遇到了相同的 404 错误。我认为 IIS 不喜欢 DELETE 请求... -
@KiranChalla 啊,这是一个非常奇怪的情况......看看下面我的答案。
标签: c# asp.net asp.net-mvc iis asp.net-web-api