Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.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# 将HttpResponse转换为HttpResponseMessage_C#_Asp.net Mvc_Asp.net Core_Asp.net Web Api2 - Fatal编程技术网

C# 将HttpResponse转换为HttpResponseMessage

C# 将HttpResponse转换为HttpResponseMessage,c#,asp.net-mvc,asp.net-core,asp.net-web-api2,C#,Asp.net Mvc,Asp.net Core,Asp.net Web Api2,我正在尝试从webapi返回pdf格式的crystalreport,这是我为is实现的 ReportDocument rd = new ReportDocument(); DataSet ds = _repo.Getdata(id); var temp = HttpContext.Current.Server.MapPath(@"\Reports\RptPrint.rpt");

我正在尝试从webapi返回pdf格式的crystalreport,这是我为is实现的

            ReportDocument rd = new ReportDocument();
            DataSet ds = _repo.Getdata(id);           
             var temp = HttpContext.Current.Server.MapPath(@"\Reports\RptPrint.rpt");
             rd.Load(temp, OpenReportMethod.OpenReportByDefault);
            rd.Refresh();
            rd.SetDataSource(ds.Tables[0]);
            rd.ExportToHttpResponse(ExportFormatType.PortableDocFormat, HttpContext.Current.Response, true, "test");
以上代码是工作文件。但现在的问题是我想在另一个应用程序mvc actionresult中使用这个webapi端点,就像这样

                 RestCallDataModel restCallResponce = APIH.APICall(_poprinturl, Enums.HttpMethods.Get);
                using (HttpClient client = new HttpClient())
                {
                    client.BaseAddress = new Uri(_poprinturl);
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                    HttpResponseMessage response = await client.GetAsync(_poprinturl);
                    if (response.IsSuccessStatusCode)
                    {
                        var data = await response.Content.ReadAsStreamAsync();
                        return new FileStreamResult(data, "application/pdf");
                    }
                }
下面这行给了我一个问题,因为api方法正在返回HttpResponse,而MVCContoler正在等待HttpResponseMessage

所以我得到以下错误

405方法不允许的错误是HTTP响应状态代码

试一试

这可能有用

HttpResponseMessage response = await client.GetAsync(_poprinturl).Result;
请参考下面的代码

public class ValuesController : ApiController
{
    public HttpResponseMessage Get()
    {
        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, "value");
        response.Content = new StringContent("hello", Encoding.Unicode);
        response.Headers.CacheControl = new CacheControlHeaderValue()
        {
            MaxAge = TimeSpan.FromMinutes(20)
        };
        return response;
    } 
}

该错误与HttpResponseMessage无关。您没有尝试返回响应,即使返回了,MVC也会将其序列化为JSON或XML字符串。你调试代码了吗?您确定GetAsync没有抛出,因为其他服务不喜欢该URL吗?您可以使用HttpClient自己的GetStreamAsync,而不是使用HttpResponseMessage。这似乎是一个。如果没有澄清您的特定问题或其他详细信息以突出显示所做的事情,很难重现问题,以便更好地理解所请求的内容。get Method Not Allowed错误可能是因为该操作需要POST请求,但您发送了get Method。CreateResponse中的结果是什么?将结果添加到HTTP响应口袋中的方法
HttpResponseMessage response = await client.GetAsync(_poprinturl).Result;
public class ValuesController : ApiController
{
    public HttpResponseMessage Get()
    {
        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, "value");
        response.Content = new StringContent("hello", Encoding.Unicode);
        response.Headers.CacheControl = new CacheControlHeaderValue()
        {
            MaxAge = TimeSpan.FromMinutes(20)
        };
        return response;
    } 
}