相关疑难解决方法(0)

从流创建字节数组

从输入流创建字节数组的首选方法是什么?

这是我目前使用.NET 3.5的解决方案.

Stream s;
byte[] b;

using (BinaryReader br = new BinaryReader(s))
{
    b = br.ReadBytes((int)s.Length);
}
Run Code Online (Sandbox Code Playgroud)

读取和写入流的块是否仍然是一个更好的主意?

c# inputstream .net-3.5

860
推荐指数
11
解决办法
82万
查看次数

如何在ASP.NET WebAPI中返回文件(FileContentResult)

在常规的MVC控制器中,我们可以用a输出pdf FileContentResult.

public FileContentResult Test(TestViewModel vm)
{
    var stream = new MemoryStream();
    //... add content to the stream.

    return File(stream.GetBuffer(), "application/pdf", "test.pdf");
}
Run Code Online (Sandbox Code Playgroud)

但是我们怎样才能把它变成一个ApiController

[HttpPost]
public IHttpActionResult Test(TestViewModel vm)
{
     //...
     return Ok(pdfOutput);
}
Run Code Online (Sandbox Code Playgroud)

这是我尝试过但它似乎不起作用.

[HttpGet]
public IHttpActionResult Test()
{
    var stream = new MemoryStream();
    //...
    var content = new StreamContent(stream);
    content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
    content.Headers.ContentLength = stream.GetBuffer().Length;
    return Ok(content);            
}
Run Code Online (Sandbox Code Playgroud)

浏览器中显示的返回结果为:

{"Headers":[{"Key":"Content-Type","Value":["application/pdf"]},{"Key":"Content-Length","Value":["152844"]}]}
Run Code Online (Sandbox Code Playgroud)

在SO上有类似的帖子:从ASP.NET Web API中的控制器返回二进制文件 .它讨论输出现有文件.但我无法使用流.

有什么建议?

c# asp.net asp.net-mvc asp.net-web-api

158
推荐指数
6
解决办法
24万
查看次数

如何从WebApi动作返回html页面?

我正在寻找一个WebApi示例,其中默认路由将给定调用者返回给定的html页面.我的路线和行动设置如下.我只想向他发送index.html页面,而不是重定向,因为他在正确的位置.

http://localhost/Site      // load index.html

// WebApiConfig.cs
config.Routes.MapHttpRoute(
    name: "Root",
    routeTemplate: "",
    defaults: new { controller = "Request", action = "Index" }
);

// RequestControlller.cs
    [HttpGet]
[ActionName("Index")]
public HttpResponseMessage Index()
{
    return Request.CreateResponse(HttpStatusCode.OK, "serve up index.html");
}
Run Code Online (Sandbox Code Playgroud)

如果我使用这个错误,那么更好的方法是什么,你能指点我一个例子吗?

WebApi 2与.NET 4.52

编辑:嗯,改进了它,但得到了json头而不是页面内容.

public HttpResponseMessage Index()
{
    var path = HttpContext.Current.Server.MapPath("~/index.html");
    var content = new StringContent(File.ReadAllText(path), Encoding.UTF8, "text/html");
    return Request.CreateResponse(HttpStatusCode.OK, content);
}

{"Headers":[{"Key":"Content-Type","Value":["text/html; charset=utf-8"]}]}
Run Code Online (Sandbox Code Playgroud)

c# iis asp.net-web-api

24
推荐指数
1
解决办法
3万
查看次数

ASP.NET Core HTTPRequestMessage返回奇怪的JSON消息

我目前正在使用ASP.NET Core RC2,我遇到了一些奇怪的结果.所以我有一个MVC控制器,具有以下功能:

public HttpResponseMessage Tunnel() {
    var message = new HttpResponseMessage(HttpStatusCode.OK);
    message.Content = new StringContent("blablabla", Encoding.UTF8);
    message.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("text/plain");
    message.Headers.CacheControl = new System.Net.Http.Headers.CacheControlHeaderValue {
        NoCache = true
    };

    return message;
}
Run Code Online (Sandbox Code Playgroud)

如果我用邮递员调用此方法并将Accept标头设置为text plain,我会收到以下回复:

{
  "Version": {
    "Major": 1,
    "Minor": 1,
    "Build": -1,
    "Revision": -1,
    "MajorRevision": -1,
    "MinorRevision": -1
  },
  "Content": {
    "Headers": [
      {
        "Key": "Content-Type",
        "Value": [
          "text/plain"
        ]
      }
    ]
  },
  "StatusCode": 200,
  "ReasonPhrase": "OK",
  "Headers": [
    {
      "Key": "Cache-Control",
      "Value": [
        "no-cache"
      ]
    }
  ], …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core-mvc asp.net-core .net-core-rc2

15
推荐指数
1
解决办法
1万
查看次数