Mat*_*t.G 5 .net c# asp.net-core-webapi asp.net-core-2.0
尝试获取.net core 2.0 Web API HttpPost方法以使用xml输入。
预期结果:从Postman调用测试端点时,输入参数(以下代码中的xmlMessage)应具有从Postman HttpPost主体发送的值。
实际结果:输入参数为空。
在Web api项目的startup.cs中,我们具有以下代码:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddXmlDataContractSerializerFormatters();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
}
Run Code Online (Sandbox Code Playgroud)
在控制器中:
[HttpPost, Route("test")]
public async Task<IActionResult> Test([FromBody] XMLMessage xmlMessage)
{
return null; //not interested in the result for now
}
Run Code Online (Sandbox Code Playgroud)
XMLMessage类:
[DataContract]
public class XMLMessage
{
public XMLMessage()
{
}
[DataMember]
public string MessageId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在邮递员标题中:
Content-Type:application/xml
Run Code Online (Sandbox Code Playgroud)
Http Post正文:
<XMLMessage>
<MessageId>testId</MessageId>
</XMLMessage>
Run Code Online (Sandbox Code Playgroud)
感谢任何可以帮助我指出正确方向的帮助。提前致谢..
启动.cs
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace test
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(config =>
{
config.InputFormatters.Add(new XmlSerializerInputFormatter());
}).AddXmlDataContractSerializerFormatters();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
}
}
Run Code Online (Sandbox Code Playgroud)
控制器.cs
using System.Runtime.Serialization;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace test.Controllers
{
[DataContract]
public class TestClass
{
[DataMember]
public string Message { get; set; }
}
[Route("[controller]")]
public class TestController : Controller
{
[HttpPost, Route("test")]
public async Task<IActionResult> Test([FromBody]TestClass test)
{
return Ok("OK");
}
}
}
Run Code Online (Sandbox Code Playgroud)
程序.cs
使用 Microsoft.AspNetCore;使用 Microsoft.AspNetCore.Hosting;
namespace test
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
}
Run Code Online (Sandbox Code Playgroud)
测试.csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.9" />
</ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3413 次 |
| 最近记录: |