如何从RestSharp获取响应数据以供用户下载?

Sam*_*lly 6 c# asp.net-mvc restsharp asp.net-mvc-3

我正在使用RestSharp通过Querystring调用HTTP服务.该服务生成Word文档.

当我调用此服务时,它看起来像是在"内容"属性中返回Word文档,但我很难弄清楚如何通过传统的下载窗口将此内容作为word文档返回给用户进行保存.

    public ActionResult DocGen(string strReportId)
    {
        var client = new RestClient("http://localhost:88");

        var request = new RestRequest("DocGen/{id}", Method.GET);
        request.AddUrlSegment("id", "1060"); // replaces matching token in request.Resource

        // execute the request
        //RestResponse response = (RestResponse) client.Execute(request);
        IRestResponse response = client.Execute(request);

        if (response.ErrorException != null)
        {
            const string message = "Error retrieving response.  Check inner details for more info.";
            var myException = new ApplicationException(message, response.ErrorException);
            throw myException;
        }

        // Important and simple line. response.rawbytes was what I was missing.
        return File(response.RawBytes,response.ContentType,"sample.doc");
    }
Run Code Online (Sandbox Code Playgroud)

这应该是一个行动吗?

内容类型似乎正确,即Word.11

那么我如何编码将这个Response.Content返回给用户呢?

提前谢谢了.

编辑

我比解决方案更接近解决方案.RestSharp的力量我猜!! 往上看.现在可能有更好的方法,我对任何建议都很满意,但这就是我目前所处的位置.

Sam*_*lly 6

return File(response.RawBytes,response.ContentType,"sample.doc");

如果有人可能受益.