Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.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
C# 如何更改ServiceStack中的默认ContentType?_C#_.net_Web Services_Rest_<img Src="//i.stack.imgur.com/WM7S8.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">servicestack - Fatal编程技术网 servicestack,C#,.net,Web Services,Rest,servicestack" /> servicestack,C#,.net,Web Services,Rest,servicestack" />

C# 如何更改ServiceStack中的默认ContentType?

C# 如何更改ServiceStack中的默认ContentType?,c#,.net,web-services,rest,servicestack,C#,.net,Web Services,Rest,servicestack,我在ServiceStack中有: appHost.ContentTypeFilters.Register("application/x-my-content-type", SerializeToStream, DeserializeFromStream); 如果客户端在http流中发送内容类型,那么一切都会按预期进行 不幸的是,我有一个客户端,它不在我的HTTP请求头控制范围内,并且不发送内容类型 如何让ServiceStack设置该路由的默认内容类型?在每个ServiceStack

我在ServiceStack中有:

appHost.ContentTypeFilters.Register("application/x-my-content-type", 
   SerializeToStream, DeserializeFromStream);
如果客户端在http流中发送内容类型,那么一切都会按预期进行

不幸的是,我有一个客户端,它不在我的HTTP请求头控制范围内,并且不发送内容类型

如何让ServiceStack设置该路由的默认内容类型?

在每个ServiceStack上列出了客户端请求特定内容类型的不同方式:

要覆盖客户端HTTPAccept标题中的内容类型,请附加?format=xml或添加。format扩展名

例如,客户端可以使用?format=x-my-content-type、添加
.x-my-content-type
扩展名或通过指定HTTP头(在HttpClient中)来指定自定义ContentType:

接受:应用程序/x-my-content-type

否则,如果您的HttpClient未发送Accept标头,则可以使用以下命令在AppHost中指定默认内容类型:

SetConfig(new HostConfig {
     DefaultContentType = "application/x-my-content-type"
});
注意:ServiceStack中的所有配置选项都设置在
HostConfig

从web浏览器调用web服务时的问题是,它们通常会要求
Accept:text/html
,如果启用,则contract ServiceStack会通过返回html来满足这一要求

为确保始终返回您的内容类型,您可能还希望通过以下方式禁用HTML功能:

SetConfig(new HostConfig {
    EnableFeatures = Feature.All.Remove(Feature.Html),
});
否则,如果您想覆盖Accept标题,您可以通过在HttpResult中修饰您的响应DTO,强制您的服务始终返回您的内容类型,即:

return new HttpResult(dto, "application/x-my-content-type");
否则,您可以在服务之外的任何地方(例如请求/响应过滤器)设置响应内容类型(Response ContentType),该类型可以访问
IHttpRequest

httpReq.ResponseContentType = "application/x-my-content-type";