c# - Web Api controller of a webforms not working to POST verb -


since newbie in webapi , searched many questions regarding this, nothing matches case. have webapi controller in webforms project.in post method, data inserted db. had done taking instructions here. done mentioned post method of controller not working

note:- 1) if use webservice(soap) same codes instead of webapi works fine. 2) if use postman testing works. 3) if use simple html page front end shows http error 405.0 - method not allowed

here 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 controller:

 public class appointmentcontroller : apicontroller     {          // post api/<controller>         [httppost]         public void post([frombody]appointment app)         {             app.save();         }      } 

here 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 web.config

<?xml version="1.0" encoding="utf-8"?> <!--   more information on how configure 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=\&quot;web\&quot; /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> 

post appointment

data: json.stringify(appointment), 

as action expecting.

also action needs return valid response demonstrated in documentation linked in original question.

public class appointmentcontroller : apicontroller {     // post api/<controller>     [httppost]     public ihttpactionresult post([frombody]appointment app) {         app.save();         return ok();     } } 

the assumption here appointment has parameterless constructor allow model binder bind , populate model.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -