我已经挣扎了4个小时,但我仍然没有得到任何解决方案。我已经应用了一些修改,但仍然我的帖子和删除api返回错误500。
GET js
$.getJSON(API_URL + 'api/claim/search', params).done(function (data) {
myJsonObject = data;
d.resolve(data);
});
return d.promise();
API
[Route("api/claim/search")]
[System.Web.Http.AcceptVerbs("GET")]
[System.Web.Http.HttpGet]
public IEnumerable<ClaimInfo> Get([FromUri] ClaimSearch obj_ClaimSearch)
{
//my code
}
此get方法正常工作
POST js
$.ajax({
type: "POST",
data: JSON.stringify(p[0]),
url: API_URL + "api/claim/" + (editorPage === "resubmission" ? "saveresubmissionpatient": "savepatient"),
contentType: "application/json",
success: function (data) {
},
error: function () {
}
});
API
[Route("api/claim/savepatient")]
[System.Web.Http.AcceptVerbs("POST")]
[System.Web.Http.HttpPost]
public Guid SavePatient([FromBody]ClaimInfo claimInfo)
{
//my code
}
这是我的WebApi.Config.cs
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
我已在我的配置中应用此网络服务器
<modules>
<remove name="WebDAVModule" />
<add type="DevExpress.Web.ASPxHttpHandlerModule, DevExpress.Web.v16.2, Version=16.2.5.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" name="ASPxHttpHandlerModule" />
</modules>
<handlers>
<remove name="WebDAV" />
<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>
答案 0 :(得分:0)
我有一个类似的问题,它与捕获请求的函数的名称有关。由于某种原因,GET函数自动执行此操作,但POST并不总是这样做。您可以尝试明确地命名它,看看它是否也解决了您的问题:
[Route("api/claim/savepatient")]
[System.Web.Http.AcceptVerbs("POST")]
[System.Web.Http.HttpPost, ActionName("SavePatient")]
public Guid SavePatient([FromBody]ClaimInfo claimInfo)
{
//my code
}
请注意,我已将[System.Web.Http.HttpPost]
更改为[System.Web.Http.HttpPost, ActionName("SavePatient")]
答案 1 :(得分:0)
您已将操作命名为“savepatient”而非Post。路由器将/ api / {controller}与您的{controller}控制器类匹配。 HTTP方法必须与类的公共方法匹配。尝试将“SavePatient”重命名为“Post”(如果使用该方法,则将“Put”重命名)。
public class ClaimController : ApiBaseController
{
//[Route("api/claim/")] don't need this
public Guid Post([FromBody]ClaimInfo claimInfo)
{
//my code to add new claiminfo
}
//[Route("api/claim/")] don't need this
public Guid Put([FromBody]ClaimInfo claimInfo)
{
//my code to edit claiminfo
}
删除网址上的额外路径:
url: API_URL + "api/claim/"