ASP.NET Web API DELETE方法405方法不允许

时间:2014-10-01 13:21:45

标签: asp.net-web-api asp.net-routing http-status-code-405 http-delete

我正在使用asp.net web api在vs 2012中开发一个项目。在这个项目中,当我想从一个单独的项目中删除一条记录时,我收到此错误消息“NetworkError:405 Method Not Allowed - {{1} }。

在客户端站点代码中。

http://localhost:30777/api/Customer/1"

- Api控制器

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

}

删除功能不起作用。其余的都很好。

- web.config中

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;

}

它似乎根本不起作用 - 我做错了什么?

0 个答案:

没有答案