我试图向我的 Controller 发送一个请求,只是为了发送一些数据,但它不起作用。它没有命中 Controller 中的 post 方法。在 google 上搜索后,我尝试了很多方法,但仍然无法正常工作。
我正在通过调用 belwo url 发布数据
邮寄: https://localhost:44341/api/FullPillarIdentifier/getIdentifierPutFileHandlingResponse
并将一些数据作为表单正文发送。
任何帮助将不胜感激
下面是 Controller 代码
[Route("api/[controller]")]
[ApiController]
public class FullPillarIdentifierController : BaseController
{
private readonly IFullPillarRepository _pillarRepository;
private readonly IXmlParser _xmlParser;
private ILogger _logger;
public FullPillarIdentifierController(ILogger logger, IFullPillarRepository pillarRepository, IXmlParser xmlParser)
{
_logger = logger;
_xmlParser = xmlParser;
_pillarRepository = pillarRepository;
}
// GET api/values
[HttpPost]
[Route("/getIdentifierPutFileHandlingResponse")]
public IActionResult CreateMessageOnQueue([FromBody] string xml)
{
try
{
IdentifierPillarForPutFileRequest identifierPillarForPutFileRequest = _xmlParser.ToObject<IdentifierPillarForPutFileRequest>(xml);
_pillarRepository.GetFileHandlingResponsePlan(identifierPillarForPutFileRequest);
return Ok("Successfull");
}
catch (Exception e)
{
_logger.Log(new CoreLogging.Logging.LogMessage
{
ActionName = MemberMetaData.MethodName(),
LoggingResponsibleSystem = "HermesWebApi",
Exceptionn = e.Message
}, Level.Error);
return Error(e.Message);
}
}
}
这是我的 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(options =>
{
options.RespectBrowserAcceptHeader = true; // false by default
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddXmlSerializerFormatters()
.AddXmlDataContractSerializerFormatters();
services.AddCors();
}
public void ConfigureContainer(ContainerBuilder builder)
{
var module = new DependencyModule();
module.Configuration = Configuration;
builder.RegisterModule(module);
}
// 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();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseCors();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
请您参考如下方法:
此外,您需要更改ILogger至 ILogger<FullPillarIdentifierController> :
[Route("api/[controller]")]
[ApiController]
public class FullPillarIdentifierController : BaseController
{
private ILogger<FullPillarIdentifierController> _logger;
private readonly IFullPillarRepository _pillarRepository;
private readonly IXmlParser _xmlParser;
public FullPillarIdentifierController(ILogger<FullPillarIdentifierController> logger, IFullPillarRepository pillarRepository, IXmlParser xmlParser)
{
_logger = logger;
_xmlParser = xmlParser;
_pillarRepository = pillarRepository;
}
[HttpPost]
[Route("getIdentifierPutFileHandlingResponse")]
public IActionResult CreateMessageOnQueue([FromBody] string xml)
{
//...
}
基础 Controller :
[Route("api/[controller]/[action]")]
public class BaseController : Controller
{
public BaseController()
{
}
//..
}
