Reputation: 2422
Since I am a newbie in webAPI , I Searched so many questions regarding this, but nothing matches my case. I have a webApi controller in webforms project.In the Post Method, some data is inserted into the DB. I had done this by taking instructions from here. Done Everything as they mentioned but the POST method of the controller is not working
Note:- 1) If i use Webservice(SOAP) with same codes instead of webAPI it works fine. 2) If I use postman for testing its works. 3) If I use simple html page for Front End it shows HTTP Error 405.0 - Method Not Allowed
Here is my aspx page
<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>
Here is my controller:
public class AppointmentController : ApiController
{
// POST api/<controller>
[HttpPost]
public void Post([FromBody]Appointment app)
{
app.Save();
}
}
Here is the 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 });
}
}
Here is my 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>
Upvotes: 0
Views: 1028
Reputation: 247521
Post the appointment
data: JSON.stringify(appointment),
as that is what the action is expecting.
Also the action needs to return a valid response as demonstrated in the documentation linked in the original question.
public class AppointmentController : ApiController {
// POST api/<controller>
[HttpPost]
public IHttpActionResult Post([FromBody]Appointment app) {
app.Save();
return Ok();
}
}
The assumption here is also that Appointment
has a parameterless constructor that will allow the model binder to properly bind and populate the model.
Upvotes: 1