0

I'm developing a project using asp.net web api in vs 2012. In this project when I want to delete a record from a separated project I get this error message "NetworkError: 405 Method Not Allowed - http://localhost:30777/api/Customer/1".

In client site code.

get: function () {
            var deferred = $q.defer();

            $http.get('`http://localhost:30777/api/Customer`')
                 .success(deferred.resolve)
                .error(deferred.reject);

            return deferred.promise;
        },

        getDetail: function (customerID) {
            var deferred = $q.defer();
            resource.get({ id: customerID },
                    function (event) {
                        deferred.resolve(event);
                    },
                    function (response) {
                        deferred.reject(response);
                    });

            return deferred.promise;
        },

        saveCustomer: function (customer) {
            $http.post('`http://localhost:30777/api/Customer/SaveCustomer`', customer).
                success(function (status) {
                    if (status == 200) {
                        alert("This customer was saved");
                    }
                    else {
                        alert("This customer was not saved")
                    }
                }).
                //error(function (data, status, headers, config) {
                //    alert("Server is busy. Please try later");
                //});

                error(function () {
                    alert("Server is busy. Please try later");
                });
        },

        deleteCustomer: function (customerId) {

            $http.delete('`http://localhost:30777/api/Customer/1`').
            success(function (data, status, headers, config) {
                if (status == 200) {
                    alert("This customer was saved");
                }
                else {
                    alert("This customer was not saved");
                }
            }).
            error(function (data, status, headers, config) {
                alert("Server is busy. Please try later");
            });
        }
get, getDetail and saveCustomer functions works fine. only deleteCustomer function didn't work at all.

In api site code

---webapiconfig file I added cross support library.


public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        var cors = new EnableCorsAttribute("http://localhost:8488", "*", "*");
        //cors.SupportsCredentials = true;
        config.EnableCors(cors);


        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

-- Api controller

public class CustomerController : ApiController
{
private readonly ICustomerServices _services;

public CustomerController(ICustomerServices services)
{
    this._services = services;
}

[HttpGet]
public IEnumerable<Customer> Get()
{
    return _services.GetAllCustomer().ToList();
}

[HttpGet]
[Route("GetCustomerById")]
public Customer GetCustomerById(string Id)
{
    return _services.GetCustomerById(Id);
}

[HttpPost]
[Route("SaveCustomer")]
public HttpResponseMessage SaveCustomer([FromBody]Customer customer)
{
    try
    {
        _services.SaveCustomer(customer);
        return new HttpResponseMessage(HttpStatusCode.OK);
    }
    catch
    {
        return new HttpResponseMessage(HttpStatusCode.InternalServerError);
    }
}

//[HttpDelete]
//[Route("RemoveCustomer")]
public void Delete(int customerId)
{
    var tt = 0;

    tt = tt + 1;

}

}

The delete function didn't work. The rest are fine.

--web.config

    <validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true">
  <remove name="WebDAVModule"/>
</modules>
<handlers>
  <remove name="WebDAV"/>
  <remove name="ExtensionlessUrlHandler-Integrated-4.0"/>
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0"/>
</handlers>

It seems it did not work at all - what am I doing wrong?

  • I guess you have tried uncommenting the [HttpDelete] and the route? – grimurd Oct 01 '14 at 19:54
  • I'm faceing same problem, let me note if you find some solution/workaround; thanks! :) – Sebastian Busek Oct 03 '14 at 13:09
  • Try the way from [Troubleshooting HTTP 405 Errors after Publishing Web API 2 Applications](http://www.asp.net/web-api/overview/testing-and-debugging/troubleshooting-http-405-errors-after-publishing-web-api-applications) – BNK Aug 29 '15 at 15:35

0 Answers0