asp.net-web-api – Ajax Post:405方法不允许

在我的API控制器中称为付款,我有以下方法
[HttpPost]
public HttpResponseMessage Charge(Payment payment)
{
    var processedPayment = _paymentProcessor.Charge(payment);
    var response = Request.CreateResponse(processedPayment.Status != "PAID" ? HttpStatusCode.ExpectationFailed : HttpStatusCode.OK,processedPayment);
    return response;
}

在我的HTML页面中,我有

$.ajax({
        type: "POST",contentType: "application/json; charset=utf-8",url: "http://localhost:65396/api/payment/charge",data: $('#addPayment').serialize(),dataType: "json",success: function (data) {
            alert(data);
        }
    });

每当我开火POST,我得到

"NetworkError: 405 Method Not Allowed - http://localhost:65396/api/payment/charge"

我失踪了什么

谢谢.

UPDATE

以下是路由信息(认)

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

            routes.MapRoute(
                name: "Default",url: "{controller}/{action}/{id}",defaults: new { controller = "Home",action = "Index",id = UrlParameter.Optional }
            );

解决方法

很可能您的路由未配置为要调用的操作.因此,请求无处不在,ASP.NET Web API发送一个空白消息“不允许的方法”.

你可以用你的路由更新问题吗?

UPDATE

和我想的一样!当您需要发送到http:// localhost:65396 / api / payment – 您的控制器称为PaymentController时,您将发送到http:// localhost:65396 / api / payment / charge.

请注意,路由没有动作.

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....