2013-12-22 81 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"