私はwebAPIの初心者ですから、これについて多くの質問を探しましたが、私の場合は何も一致しません。 webformsプロジェクトにwebApiコントローラがあります。ポストメソッドでは、いくつかのデータがDBに挿入されます。私はhereから指示を取ってこれを行った。私は代わりに、それが正常に動作WEBAPIの同じコードでWebサービス(SOAP)を使用している場合 1) - :彼らは言及したが、コントローラのPOSTメソッドがwebformsのWeb ApiコントローラがPOST動詞に機能しない
注動作していない、すべてを完了。 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>
こんにちは..同じホストにいますか? ..webformとwebapiは同じlocalhost:portで動作しますか? –
あなたは本当に行うことができます:JSON.stringify(予定) –
はい両方が同じホストにあります。 –