2

I have been trying to get DELETE working now for over an hour and I am a little frustrated. I have tried every recommendation I can find. I have removed EVERY reference to ANYTHING WEBDAV from my applicationconfig as well as copying my whole webserver section over. I did it in steps and tried different variations and I still cannot get it to work. Yes my method is called DELETE and I added the [HttpDelete] to further make sure it knew I wanted delete support. Any other suggestions? :/

<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>

Tried these and more:

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

Kinda ridiculous, if M$ is going to release a product they should make sure its configured to work as expected out of the box.

Here is the controller code, it doesn't even break in it though as the application "doesnt understand the DELETE verb". The HttpDelete I slapped on there is from System.Web.Http not the mvc one. The name is by convention but I was hoping if I stuck the attribute on it would understand anyways.

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

This is in my angularJs controller

    $scope.deleteProduct = function (product) {

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

This is in my angular service using $resource. (The rest of my crud is working)

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

This is my routing inside the 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):/);
        }]
);
Community
  • 1
  • 1
Tony
  • 3,269
  • 1
  • 27
  • 48

2 Answers2

1

It looks a lot in your Web.config. Can you trim it to...

<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>

I have tested it in IIS with WebDAV component installed and both PUT and DELETE verbs work with it.

Rajiv
  • 1,426
  • 14
  • 21
0

I was stuck in a position similar to this but with a with PUT verb when I suddenly realize, after requesting my Api via Fiddler Composer, that my restClient was performing an unvalid request like using

"api/resource?id=1234" 

instead of

"api/resource/1234"
Guillaume Raymond
  • 1,726
  • 1
  • 20
  • 33