返回“允许” WCF Web API 中带有 HTTPResponseMessage 的实体标头

发布于 2024-12-24 02:31:04 字数 1876 浏览 2 评论 0原文

我试图在响应消息中返回“允许”实体标头,但不断收到以下消息:

{“无法添加标头。请确保将请求标头添加到 HttpRequestMessage,将响应标头添加到 HttpResponseMessage,将内容标头添加到HttpContent 对象。"}

这是代码片段:

 [WebInvoke(UriTemplate = "{id}", Method = "DELETE")]
        public HttpResponseMessage<Order> DeleteOrder(int id)
        {
            HttpResponseMessage<Order> response = null;

            try
            {
                if (id <= 0)
                {
                    response = new HttpResponseMessage<Order>(HttpStatusCode.BadRequest);
                }
                else
                {
                    // For brevity, I'm assuming that order - 123456 was already served and logged. Hence it cannot
                    // be deleted. Order 12345, however, can be deleted.
                    // Note: The code doesn't actual delete anything. This is just a demonstration of
                    // the DELETE verb
                    if (id == 12345)
                    {                      
                        return new HttpResponseMessage<Order>(HttpStatusCode.NoContent);
                    }

                    if (id == 123456)
                    {                       
                        response = new HttpResponseMessage<Order>(HttpStatusCode.MethodNotAllowed);
                        response.Headers.AddWithoutValidation("Allow", "PUT");
                    }

                    // return '404 - Not Found' status code
                    response = new HttpResponseMessage<Order>(HttpStatusCode.NotFound);

                }

                return response;
            }
            catch (Exception ex)
            {
                return response = new HttpResponseMessage<Order>(HttpStatusCode.InternalServerError);
            }
        }

任何建议都会非常有帮助。

谢谢,

多尔曼

I'm trying to return the 'Allow' entity header within the response message and I keep getting the following message:

{"The header cannot be added. Make sure to add request headers to HttpRequestMessage, response headers to HttpResponseMessage, and content headers to HttpContent objects."}

Here's the code snippet:

 [WebInvoke(UriTemplate = "{id}", Method = "DELETE")]
        public HttpResponseMessage<Order> DeleteOrder(int id)
        {
            HttpResponseMessage<Order> response = null;

            try
            {
                if (id <= 0)
                {
                    response = new HttpResponseMessage<Order>(HttpStatusCode.BadRequest);
                }
                else
                {
                    // For brevity, I'm assuming that order - 123456 was already served and logged. Hence it cannot
                    // be deleted. Order 12345, however, can be deleted.
                    // Note: The code doesn't actual delete anything. This is just a demonstration of
                    // the DELETE verb
                    if (id == 12345)
                    {                      
                        return new HttpResponseMessage<Order>(HttpStatusCode.NoContent);
                    }

                    if (id == 123456)
                    {                       
                        response = new HttpResponseMessage<Order>(HttpStatusCode.MethodNotAllowed);
                        response.Headers.AddWithoutValidation("Allow", "PUT");
                    }

                    // return '404 - Not Found' status code
                    response = new HttpResponseMessage<Order>(HttpStatusCode.NotFound);

                }

                return response;
            }
            catch (Exception ex)
            {
                return response = new HttpResponseMessage<Order>(HttpStatusCode.InternalServerError);
            }
        }

Any advice would be very helpful.

Thanks,

dorman

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别靠近我心 2024-12-31 02:31:04

尝试使用response.Content.Headers 代替。

Try response.Content.Headers instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文