[英]How to add the Content-Type header to a .Net GET web request?
[英]REST WEB API: How to make Content-Type header optional?
我有Web API,並且希望使內容類型可選。
當前,如果我在調用REST(POST)方法時忽略了content-Type標頭,則會引發錯誤。
這就是我在Web API中定義路由的方式-
routes.MapHttpRoute(
name: "myMethodPost",
routeTemplate: "api/{id}/Settings/{settings}",
defaults: new { controller = "Settings", action = "updateSettings" },
constraints: new { httpMethod = new HttpMethodConstraint("POST") }
);
在調用此方法時-
api/1/Settings/testSettings
method = "Post"
它引發錯誤。 但是當添加以下內容時-
apiRequest.ContentType = "application/json";
它工作正常。
現在,如何使Content-Type標頭為可選?
您可以在服務器上創建一個消息處理程序,如果內容類型丟失並且正文長度為非零,則會將Content-Type設置為“ application / json”。 這樣,客戶端就不必發送它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.