[英]Method not allowed doing a post in WebAPI
I have a controller defined as follows
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace MissingClinics.Controllers
{
[RoutePrefix("api/appointment")]
public class AppointmentController : ApiController
{
[HttpPost]
public IHttpActionResult GetMissingKeys([FromBody]String MRNList)
{
return Ok();
}
}
}
and the following JavaScript calls that page. 然后以下JavaScript调用该页面。
try {
$.ajax({
type: 'POST',
url: '/api/Appointment/GetMissingKeys',
data: $('#mrnList').val(),
dataType: 'text'
}).done(function () {
alert('done!');
}).fail(function (Status, errorThrown) {
alert('Error: ' + Status.status + ' - ' + Status.statusText);
}).always(function () {
alert('All done or not');
});
}
catch (e) {
alert(e);
}
WebApi.config is the following WebApi.config是以下
namespace MissingClinics
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
RouteConfig.cs RouteConfig.cs
namespace MissingClinics
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
Now because I am going to be passing a fair amount of data I need to pass it in the body (rather than as part of the Url). 现在,因为我要传递大量数据,所以我需要在主体中传递它(而不是作为Url的一部分)。
But in the Call it is returning Error 405 - Method not allowed. 但是在调用中它返回错误405-不允许使用方法。 But from what I can see jquery should be making a post request and the controller is accepting a post - so why the Method not allowed?
但是从我可以看到,jQuery应该发出一个发布请求,而控制器正在接受一个发布-那么为什么不允许该Method?
With the default routing template, Web API uses the HTTP method to select the action. 使用默认的路由模板,Web API使用HTTP方法来选择操作。 However, you can also create a route where the action name is included in the URI:
但是,您也可以创建一条路径,其中将动作名称包含在URI中:
routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Been a while since i've used jquery but 自从我使用jQuery以来已经有一段时间了,但是
Shouldn't this 这不应该吗
data: $('#mrnList').val(),
be this 是这个
data : {mrnList: $('#mrnList').val()}, //Or MrnList depending on your jsonformatting options
Also the dataType should be 'json'
i think. 我认为dataType也应该是
'json'
。
Have you tried adding [Route("GetMissingKeys")]
to your method? 您是否尝试过将
[Route("GetMissingKeys")]
到您的方法中?
Have you changed the default route from api/{action}
to api/{controller}/{action}/{id}
? 您是否已将默认路由从
api/{action}
更改为api/{controller}/{action}/{id}
?
ASP.NET WEB API select action with request HTTP method,there are two ways below: 使用请求HTTP方法的ASP.NET WEB API选择操作,有以下两种方法:
1.Change the web api default route 1.更改Web API默认路由
WebApiConfig.cs WebApiConfig.cs
config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } );
With this ,you do not need to change you html file. 有了这个,您不需要更改您的html文件。
2.Change your action name 2.更改动作名称
public IHttpActionResult Post([FromBody]String MRNList)
and call javascript like this : 并像这样调用javascript:
$.ajax("/api/Appointment", {type:"POST", data:$("").val() ... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.