![](/img/trans.png)
[英]ASP.NET Web API: "Message": "The requested resource does not support http method 'POST', PUT, DELETE." in Postman
[英]ASP.NET Web Api “The requested resource does not support http method PUT/DELETE”
我正在使用ASP.NET/C#構建API。 但是,當嘗試發出PUT或DELETE請求時,出現以下錯誤:
"The requested resource does not support http method PUT (or DELETE)"
我知道之前已經討論過這個問題; 但是,我已經查看了對相關問題的答復(包括此答復),但尚未找到解決方案。 我已禁用WebDAV,並確保在ExtensionlessUrlHanlder中允許使用動詞。 我的web.config的“網絡服務器”部分如下:
<system.webServer>
<modules>
<remove name="FormsAuthentication" />
<remove name="WebDAVModule"/>
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />
</modules>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="WebDAV" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
<validation validateIntegratedModeConfiguration="false" />
</system.webServer>
控制器如下:
namespace MidamAPI.Controllers
{
[RoutePrefix("SupplyItems")]
public class SupplyItemsController : ApiController
{
UnitOfWork worker = new UnitOfWork();
[Route("")]
[HttpGet]
public string Get()
{
IEnumerable<SupplyItemsDTO> dtoList = Mapper.Map<List<SupplyItem>, List<SupplyItemsDTO>>(worker.SupplyItemRepo.Get().ToList());
return JsonConvert.SerializeObject(dtoList);
}
[Route("{propertyType}")]
public string Get(String propertyType = "BK")
{
IEnumerable<SupplyItemsDTO> dtoList = null;
if (propertyType.Equals("POPEYES", StringComparison.CurrentCultureIgnoreCase))
{dtoList = Mapper.Map<List<PopeyesSupplyItem>, List<SupplyItemsDTO>>(worker.PopeyesItemRepo.Get().ToList());
}
dtoList = Mapper.Map<List<BKSupplyItem>, List<SupplyItemsDTO>>(worker.BKItemRepo.Get().ToList());
return JsonConvert.SerializeObject(dtoList);
}
[Route("{id:int}")]
public string Get(int id)
{
SupplyItemsDTO dto = Mapper.Map<SupplyItem, SupplyItemsDTO>(worker.SupplyItemRepo.GetByID(id));
return JsonConvert.SerializeObject(dto);
}
[Route("")]
[HttpPost]
public HttpResponseMessage Post([FromBody]SupplyItem itm)
{
try
{
worker.SupplyItemRepo.Insert(itm);
worker.Save();
return Request.CreateResponse(HttpStatusCode.OK, itm);
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.BadRequest, ex);
}
}
[Route("")]
[HttpDelete]
public HttpResponseMessage Delete(int id)
{
try
{
SupplyItem itm = worker.SupplyItemRepo.GetByID(id);
worker.SupplyItemRepo.Delete(itm);
worker.Save();
return Request.CreateResponse(HttpStatusCode.OK, itm);
}
catch(Exception ex)
{
return Request.CreateResponse(HttpStatusCode.BadRequest, ex);
}
}
[Route("")]
[HttpPut]
public HttpResponseMessage Put(int id, SupplyItem item) {
try
{
item.ID = id;
worker.SupplyItemRepo.Update(item);
return Request.CreateResponse(HttpStatusCode.OK, item);
}
catch(Exception ex)
{
return Request.CreateResponse(HttpStatusCode.BadRequest, ex);
}
}
}
}
GET和POST調用按預期方式工作。 我不想更改applicationhost.config文件,實際上不希望以任何方式修改Web服務器(因為這是我的開發機器),或者使用可能表示安全漏洞的標頭。
響應頭:
Access-Control-Allow-Credentials →true
Access-Control-Allow-Headers →Origin, X-Requested-With, Content-Type, Accept, X-Token,Authorization
Access-Control-Allow-Methods →GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Methods →GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin →*
Allow →GET
Cache-Control →no-cache
Content-Length →72
Content-Type →application/json; charset=utf-8
Date →Tue, 10 Oct 2017 13:39:03 GMT
Expires →-1
Pragma →no-cache
Server →Microsoft-IIS/8.0
X-AspNet-Version →4.0.30319
X-Powered-By →ASP.NET
X-SourceFiles →=?UTF-8?B?QzpcVmlzdWFsIFN0dWRpbyBQcm9qZWN0c1xNaWRhbWVyaWNhQVBJXE1pZGFtQVBJXHN1cHBseWl0ZW1zXDQ1NA==?=
IIS日志中的請求:
2017-10-10 13:27:35 ::1 PUT /supplyitems/454 - 57263 - ::1 PostmanRuntime/6.3.2 - 405 0 0 0
我的路線:
config.Routes.MapHttpRoute(
name: "DefaultAPI",
routeTemplate: "{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
任何建議表示贊賞。 我正在使用IIS Express。 謝謝。
您可能仍然安裝了一些額外的模塊。 即使您已解決這些問題,您的請求和路線也將不匹配。
您的路由配置已被忽略,因為您已使用路由屬性明確設置了路由。 您應該將DELETE方法的route屬性更新為[Route("{id:int}")]
。 對於PUT方法,尚不清楚您在做什么,但是我認為您會想要執行以下操作: [Route("{id:int}")] [HttpPut] public HttpResponseMessage Put([FromUrl]int id, [FromBody]SupplyItem item) { ...
對我來說,有這個錯誤的冒險是:
之所以會得到這種結果,可能是因為您安裝了一些額外的IIS模塊: WebDAV
如果不需要它,我會從IIS中刪除它。
該模塊是IIS角色的一部分。 要消除此問題,請轉到:Windows功能->打開/關閉Windows功能-> Internet信息服務->萬維網服務->通用HTTP功能-> WebDAV發布。
如果要繼續使用,則需要將PUT / DELETE添加到ISAPI過濾器中。
有關此處的更多說明: https : //stackoverflow.com/a/33552821/4869329
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.