【发布时间】:2020-01-18 09:05:10
【问题描述】:
您好,我正在尝试在 asp.net core 2.2 中为 slack 命令构建一个端点。 我有一个数据结构表示来自 slack 的命令请求,如下所示:
public class SlackCommandDTO
{
[FromForm(Name = "token")]
public string Token { get; set; }
[FromForm(Name = "team_id")]
public string TeamId { get; set; }
[FromForm(Name = "team_domain")]
public string TeamDomain { get; set; }
[FromForm(Name = "channel_id")]
public string ChannelId { get; set; }
[FromForm(Name = "channel_name")]
public string ChannelName { get; set; }
[FromForm(Name = "user_id")]
public string UserId { get; set; }
[FromForm(Name = "user_name")]
public string UserName { get; set; }
[FromForm(Name = "command")]
public string Command { get; set; }
[FromForm(Name = "text")]
public string Text { get; set; }
[FromForm(Name = "response_url")]
public string ResponseUrl { get; set; }
[FromForm(Name = "trigger_id")]
public string TriggerId { get; set; }
}
我的控制器接收数据如下所示:
[Route("api/[controller]")]
[ApiController]
public class CustomerServiceController : ControllerBase
{
// POST api/customerservice
[HttpPost]
public void Post([FromForm] SlackCommandDTO command)
{
Console.Write(command.Token);
}
}
我的 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().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
// 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();
}
}
我已尝试将 startup.cs 中的兼容性设置设置为 2.1 和 2.2。
结果始终是所有属性都包含 null 的对象的实例。
我已尝试将装饰器设置为 [FromBody](这不是应该的),但在这种情况下,我会得到 415 不受支持的媒体类型(应该如此)。
我尝试使用内容类型 x-www-form-urlencoded 和 form-data 以及 text/plain 和 application/json 发送请求。后两者返回 415。
我也尝试通过 swagger 发送请求,结果相同,并且对每对数据都使用 -d 关键字和 -F 关键字进行 curl。
如果我遗漏了一些信息,请告诉我,我在这里画了一个关于如何解决它的空白,所以请帮忙。
根据这篇关于在 slack 中实现斜杠命令的文章,我收到的数据来自 Slack。 https://api.slack.com/slash-commands#responding_to_commands
【问题讨论】:
-
我不能请求来自 Slack.com 并且格式为 x-www-form-urlencoded
-
你能显示 Startup.ConfigureServices 吗?你使用 AddMvc 还是 AddMvcCore?
-
这是未更改的默认 webapi 没有身份的 asp.net core 2.2 我目前在公共交通工具上,所以我无法上传代码。我会在几个小时内完成。
标签: asp.net-web-api postman slack asp.net-core-2.2 slack-commands