ServiceStack 并返回一个流 [英] ServiceStack and returning a stream

查看:61
本文介绍了ServiceStack 并返回一个流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用 ServiceStack,这是一个了不起的库.

I have just started to use ServiceStack which is an amazing library.

但是,我有一个业务需求,我们必须返回 xml 和 json,其中 xml 必须采用特定格式.

However, I have a business requirement where we must return xml and json where the xml must be in specific format.

例如,我们现有的客户希望使用以下格式的 xml:

For example we have existing customers that expect xml of the format:

<service name="service1" type="audio" .../>

所以基本上是一堆属性.

so basically a bunch of attributes.

我知道 ServiceStack 使用 DTO 的概念并使用 DataContractSerializer 返回 xml 元素,而不是上面带有 xml 属性的表单.

I know that ServiceStack uses concepts of DTOs and uses the DataContractSerializer which returns xml elements rather than in the form above with xml attributes.

我仍然想对请求使用 DTO(在 Accept 标头中传入 application/xml 或 application/json),然后我可以创建自己的 xml 字符串或 json 字符串,然后将它们作为 :

I still want to use the DTOs for requests (passing in application/xml or application/json in the Accept header) and I can then create my own xml strings or json strings and then return them as :

string result = "....xml or json string...";
return new MemoryStream(Encoding.UTF8.GetBytes(result));

其中结果字符串可以是 xml 字符串或 json 字符串.

where the result string could be an xml string or a json string.

我在 fiddler 中注意到响应 Content-Type 为 text/html.

I noticed in fiddler the response Content-Type as text/html.

我使用的方法是否违反了任何 REST 原则?当前的 Content-Type (text/html) 会不会有问题?

With the approach I am using, am I violating any REST principles? Will there be issues with the Content-Type as it is currently (text/html)?

如果我确实使用这种方法,它确实可以解决业务需求.

If I do use this approach it does solve the business requirements.

编辑

我发现我可以将 httpResult 返回为:

I found that I can return a httpResult as :

return new HttpResult(
         new MemoryStream(Encoding.UTF8.GetBytes(result)), "application/xml");

给出正确的内容类型.

那么,这是正确的方式吗,还是如果我走这条路会遇到问题?

So, is this the right way or will I have issues if I go down this route?

推荐答案

是的,返回 IHttpResult 将允许您根据需要控制确切的负载、内容类型和其他 HTTP 标头.

Yes returning an IHttpResult will allow you to control the exact payload, Content-Type and other HTTP Headers as you wish.

此外,您不仅限于返回流,即您可以使用 HttpResult 返回具有不同 Content-Type 的 xml 字符串.

Also you're not limited to returning a stream, i.e. you can use the HttpResult to return an xml string with a different Content-Type.

这里有一些链接可以更好地展示 ServiceStack 返回类型的可能性:

Here are some links that better demonstrate what's possible with ServiceStack return types:

您还可以使用 FileInfo 对象返回静态文件,并使用可选参数将文件作为附件返回,这将提示用户下载文件而不是尝试在浏览器中查看:

You can also return a static File using a FileInfo object, with an optional parameter to return the file as an attachment which will prompt the user to download the file rather than attempt to view it in the browser with:

return new HttpResult(new FileInfo("~/static-response.xml"), asAttachment:true);

这篇关于ServiceStack 并返回一个流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆