【问题标题】:WebAPI 405 error on DELETE删除时出现 WebAPI 405 错误
【发布时间】:2014-01-10 15:57:56
【问题描述】:

我一直试图让 DELETE 现在工作一个多小时,我有点沮丧。我已经尝试了我能找到的每一个建议。我已经从我的应用程序配置中删除了对任何 WEBDAV 的所有引用,并复制了我的整个网络服务器部分。我分步完成并尝试了不同的变化,但我仍然无法让它工作。是的,我的方法称为 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):/);
        }]
);

【问题讨论】:

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


【解决方案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 动词都可以使用它。

【讨论】:

    【解决方案2】:

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

    "api/resource?id=1234" 
    

    而不是

    "api/resource/1234"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-16
      • 1970-01-01
      • 2017-05-22
      • 1970-01-01
      • 2023-04-07
      • 2014-10-27
      • 2023-03-22
      相关资源
      最近更新 更多