2013-12-22 86 views
1

我一直试图让DELETE工作一个多小时,而且我有点沮丧。我已经尝试了所有我能找到的建议。我已经从我的applicationconfig中删除了对任何WEBDAV的引用,并且复制了我的整个webserver部分。我一步一步做了,并尝试了不同的变化,我仍然无法实现它的工作。是的,我的方法被称为DELETE,并添加了[HttpDelete]以进一步确保它知道我想要删除支持。还有其他建议吗? :/DELETE上的WebAPI 405错误

<system.webServer> 
    <validation validateIntegratedModeConfiguration="false" /> 
    <modules runAllManagedModulesForAllRequests="true"> 
     <remove name="UrlRoutingModule-4.0" /> 
     <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" /> 
     <remove name="FormsAuthenticationModule" /> 
     <remove name="WebDAVModule" /> 
    </modules> 
    <httpProtocol> 
     <customHeaders> 
     <add name="Access-Control-Allow-Origin" value="*" /> 
     <add name="Access-Control-Allow-Headers" value="Content-Type" /> 
     <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" /> 
     </customHeaders> 
    </httpProtocol> 
    <handlers> 
     <remove name="WebDAV" /> 
     <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> 
     <remove name="OPTIONSVerbHandler" /> 
     <remove name="TRACEVerbHandler" /> 
     <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" /> 
     <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" /> 
     <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> 
     <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" /> 
     <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" /> 
     <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> 
    </handlers> 
    </system.webServer> 

尝试这些和更多:

Asp.NET Web API - 405 - HTTP verb used to access this page is not allowed - how to set handler mappings

Web API Put Request generates an Http 405 Method Not Allowed error

http://www.britishdeveloper.co.uk/2010/06/dont-use-modules-runallmanagedmodulesfo.html

有点儿可笑,如果M $即将推出一个产品,他们应该确保其配置为按照预期的方式工作。

这里是控制器代码,它甚至没有打破它,虽然作为应用程序“不明白DELETE动词”。 HttpDelete我打了就是从System.Web.Http而不是mvc。名字是按惯例,但我希望如果我坚持它的属性会理解无论如何。

// DELETE api/product/5 
    [HttpDelete] 
    public HttpResponseMessage Delete(int id) 
    { 
     HttpResponseMessage response = null; 

     var product = _uow.Repository<Product>().ById(id); 

     if (product == null) 
      response = Request.CreateResponse(HttpStatusCode.NotFound); 

     _uow.Repository<Product>().Delete(id); 
     try 
     { 
      _uow.Save(); 
     } 
     catch (DbUpdateConcurrencyException ex) 
     { 
      response = Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); 
     } 

     var model = Mapper.Map<ProductModel>(product); 
     return response ?? Request.CreateResponse(HttpStatusCode.OK, model); 
    } 

这是我angularJs控制器

$scope.deleteProduct = function (product) { 

     product.$delete(); 
     dismiss(); 
     //$scope.products = _.without($scope.products, product); 
    }; 

这是我的角度服务使用$资源。 (我的CRUD的其余工作)

minopApp.services.factory("ProductSvc", ['$resource', 
function ($resource) { 
    return $resource('/api/product/:id', 
     { productId: '@id' }, 
     { 
      update: { method: 'PUT' } 
     }); 
}]).value('version', '0.1'); 

这是我的我被困在一个类似的位置,app.js

minopApp.config(
    ['$routeProvider', 
    function ($routeProvider) { 
     $routeProvider. 
      when('/products', { 
       templateUrl: 'areas/product/product-list.html', 
       controller: 'ProductCtrl' 
      }). 
      when('/products/new', { 
       templateUrl: 'areas/product/product-create.html', 
       controller: 'ProductCtrl' 
      }). 
      when('/products/:id', { 
       templateUrl: 'areas/product/product-detail.html', 
       controller: 'ProductCtrl' 
      }). 
      when('/products/:id/edit', { 
       templateUrl: 'areas/product/product-edit.html', 
       controller: 'ProductCtrl' 
      }). 
      otherwise({ 
       redirectTo: '/products' 
      }); 
    }], 
    ['$compileProvider', 
     function ($compileProvider) { 
      $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|tel):/); 
     }] 
); 
+0

您能否提供Web API控制器的代码和执行DELETE请求的代码? –

+0

选中此项:http://stackoverflow.com/questions/20260209/method-delete-is-not-allowed-by-access-control-allow-methods-error/30649935#30649935 –

回答

0

内,但有带PUT动词路由时,我突然实现,通过请求作曲提琴手我的API后,我的RESTClient实现是喜欢用

"api/resource?id=1234" 

,而不是

执行unvalid请求
"api/resource/1234"