[英]REST WEB API: How to make Content-Type header optional?
I have Web API and would like to make the content type optional. 我有Web API,并且希望使内容类型可选。
Currently if I ignored content-Type header while calling REST (POST) Method it throws error. 当前,如果我在调用REST(POST)方法时忽略了content-Type标头,则会引发错误。
This is how I defined route in Web API - 这就是我在Web API中定义路由的方式-
routes.MapHttpRoute(
name: "myMethodPost",
routeTemplate: "api/{id}/Settings/{settings}",
defaults: new { controller = "Settings", action = "updateSettings" },
constraints: new { httpMethod = new HttpMethodConstraint("POST") }
);
while calling this method - 在调用此方法时-
api/1/Settings/testSettings
method = "Post"
it throws error. 它引发错误。 but when added following -
但是当添加以下内容时-
apiRequest.ContentType = "application/json";
it is working fine. 它工作正常。
Now, how to make Content-Type header optional? 现在,如何使Content-Type标头为可选?
You could create a messagehandler on the server that will set the Content-Type to 'application/json' if it is missing and there is a non-zero length body. 您可以在服务器上创建一个消息处理程序,如果内容类型丢失并且正文长度为非零,则会将Content-Type设置为“ application / json”。 That way the client doesn't have to send it.
这样,客户端就不必发送它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.