DELETE上的Web API 405错误

时间:2013-12-22 05:18:20

标签: c# angularjs rest iis asp.net-web-api

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

<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);
    };

这是我使用$ resource的角度服务。 (我的其余部分正在运作)

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):/);
        }]
);

2 个答案:

答案 0 :(得分:1)

在您的Web.config中看起来很多。你能修剪到...

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true" runManagedModulesForWebDavRequests="true">
      <remove name="WebDAVModule" />
    </modules>
    <handlers>
      <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>

我已经在安装了WebDAV组件的IIS中对其进行了测试,并且PUT和DELETE动词都可以使用它。

答案 1 :(得分:0)

当我突然意识到,在通过Fiddler Composer请求我的Api之后,我突然意识到我的restClient正在执行一个无效的请求,例如使用

"api/resource?id=1234" 

而不是

"api/resource/1234"