Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bitbucket POST挂钩与.Net ApiController设置?_.net_Asp.net Web Api Routing_Asp.net Apicontroller - Fatal编程技术网

Bitbucket POST挂钩与.Net ApiController设置?

Bitbucket POST挂钩与.Net ApiController设置?,.net,asp.net-web-api-routing,asp.net-apicontroller,.net,Asp.net Web Api Routing,Asp.net Apicontroller,我正在尝试让我的.NETWebAPI 2.2接受来自bitbucket的帖子。但是BB并没有将其数据作为主体发布,而是作为一个post参数发布 我在这里用邮递员模拟了bitbucket发送的内容: 您可以在此处导入邮递员收藏: 当我试图发布到我的ApiController时,我得到 { "Message": "The requested resource does not support http method 'POST'." } 当我的post函数签名如下所示时会发生这种情况:

我正在尝试让我的.NETWebAPI 2.2接受来自bitbucket的帖子。但是BB并没有将其数据作为主体发布,而是作为一个post参数发布

我在这里用邮递员模拟了bitbucket发送的内容:

您可以在此处导入邮递员收藏:

当我试图发布到我的ApiController时,我得到

{
    "Message": "The requested resource does not support http method 'POST'."
}
当我的post函数签名如下所示时会发生这种情况:

public string Post([FromUri]string payload)
{
    var hookEvent = JsonConvert.DeserializeObject<HookEvent>(payload);
    ....
有效负载不是空的,但其中的所有字段都是空的

    public string Post(HookEvent payload)
    {
给出此错误:

"Message": "The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource.",
"ExceptionMessage": "No MediaTypeFormatter is available to read an object of type 'HookEvent' from content with media type 'application/octet-stream'.",
"ExceptionType": "System.Net.Http.UnsupportedMediaTypeException",
"StackTrace": "   at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n   at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n   at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"
我知道我的“HookEvent”正在正确地反序列化,因为如果更改PostMan以将负载作为请求主体发送,则对象将正确地反序列化,并设置了所有字段。不幸的是,BB没有以这种方式发送有效负载,而是作为post参数

我的控制器签名是:

public class BitbucketHookController : ApiController
{
    ....
其他路由位:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapMvcAttributeRoutes();
    }
}
以及Global.asax.cs中的应用程序_Start()

        DiConfig.Register();
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

我必须做什么才能让我的控制器接受post有效负载?

我能够提交的最佳解决方案是使用容器模型

public class HookEventContainer
{
    public string payload { get; set; }
    public HookEvent HookEvent {
        get
        {
            if (string.IsNullOrEmpty(payload))
            {
                return null;
            }
            return JsonConvert.DeserializeObject<HookEvent>(payload);
        }
    }
}
public类HookEventContainer
{
公共字符串有效负载{get;set;}
公共厕所{
收到
{
if(string.IsNullOrEmpty(有效负载))
{
返回null;
}
返回JsonConvert.DeserializeObject(有效负载);
}
}
}
然后用这个作为我的帖子签名:

    // POST api/<controller>
    //
    public string Post(HookEventContainer eventData)
    {
        var hookEvent = eventData.HookEvent;
//POST api/
//
公共字符串Post(HookEventContainer事件数据)
{
var hookEvent=eventData.hookEvent;
虽然很难看,但很管用


我还向Bitbucket发送了一个请求,以了解他们为什么选择将有效负载作为参数而不是请求正文发送。由于Atlassian购买了它们,它们的响应性并不那么强……

思考1,对于第一个不返回方法post的方法,您能否用
[HttpPost]来修饰它
Attribute-看看我们是否能强迫它接收帖子。思考2,在你的方法中,你的模型有空值-你能检查原始请求,看看传递的是什么以及如何传递。你可以使用类似
var req=request.Content.ReadAsByteArrayAsync()的东西访问原始请求
re Think 1:添加装饰不会改变结果,我仍然会收到一个错误消息,因为请求的资源不支持http方法“POST”。re Think 2:我使用Request.Content.ReadAsStringAsync();在将数据转换为字符串后保存。此调用返回的结果字段包含一个字符串“有效载荷=%7B++++++%22canon_url%22%3A++%22https%3……。”这是urlendcoded json有效载荷。如果我将该值放入url解码器中,它将变为:“有效载荷={”canon_url:“https:……。”。我的意思是,我想我可以手动剥离有效负载=,然后手动反序列化JSON,但为什么ApicController不发挥它的神奇作用。JSON seralizer已注册?
configuration.Formatters.Add(new JsonMediaTypeFormatter());
    // POST api/<controller>
    //
    public string Post(HookEventContainer eventData)
    {
        var hookEvent = eventData.HookEvent;