由于我是webAPI的新手,我搜索了很多关于此问题的问题,但没有任何内容符合我的情况。 我在webforms项目中有一个webApi控制器。在Post方法中,一些数据被插入到DB中。我是通过接受here的指示完成的。完成他们提到的一切,但控制器的POST方法不起作用
注意: - 1)如果我使用相同代码而不是webAPI的Webservice(SOAP),它可以正常工作。 2)如果我使用邮递员测试其作品。 3)如果我使用前端的简单html页面显示 HTTP错误405.0 - 方法不允许
这是我的aspx页面
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="name" />
<input type="text" id="lastname" />
<button id="submit"></button>
<script>
$(document).ready(function () {
$('#submit').click(function () {
var appointment = {};
appointment.FirstName = $('#name').val();
appointment.LastName = $('#lastname').val();
$.ajax({
url: '/api/Appointment',
method: 'POST',
dataType: 'JSON',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ 'app': appointment }),
success: function ()
{
alert('success');
},
error:function(xhr,err)
{
alert(xhr.responseText);
}
});
});
});
</script>
</div>
</form>
</body>
这是我的控制器:
public class AppointmentController : ApiController
{
// POST api/<controller>
[HttpPost]
public void Post([FromBody]Appointment app)
{
app.Save();
}
}
这是Global.asax
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = System.Web.Http.RouteParameter.Optional });
}
}
这是我的web.config
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<add name="connection" connectionString="server=MACLINKSERVER\MSSQL_DEV;Database=DB_A1DE96_Smartgdx;UID=sa;PWD=123;" providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
<httpModules>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
</httpModules>
<webServices>
<protocols>
<add name="HttpGet" />
<add name="HttpPost" />
</protocols>
</webServices>
</system.web>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
</compilers>
</system.codedom>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules>
<remove name="ApplicationInsightsWebTracking" />
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
</modules>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers></system.webServer>
</configuration>
答案 0 :(得分:1)
发布约会
data: JSON.stringify(appointment),
因为这就是行动所期待的。
此操作还需要返回有效的响应,如原始问题中链接的文档中所示。
public class AppointmentController : ApiController {
// POST api/<controller>
[HttpPost]
public IHttpActionResult Post([FromBody]Appointment app) {
app.Save();
return Ok();
}
}
此处的假设还是Appointment
具有无参数构造函数,允许模型绑定器正确绑定和填充模型。