Asp.net web api 我们如何将内容类型设置为";“纯文本/纯文本”;在asp.net web api中
我们在应用程序中使用asp.net web api,因为我们试图以文本/普通格式返回内容类型的响应,但我们无法成功。和我们在ASP.NET MVC上尝试的一样,它工作正常。您能为我提供Web API中的等效解决方案吗 请在下面查找在ASP.NET MVC中实现的函数Asp.net web api 我们如何将内容类型设置为";“纯文本/纯文本”;在asp.net web api中,asp.net-web-api,httpresponse,content-type,Asp.net Web Api,Httpresponse,Content Type,我们在应用程序中使用asp.net web api,因为我们试图以文本/普通格式返回内容类型的响应,但我们无法成功。和我们在ASP.NET MVC上尝试的一样,它工作正常。您能为我提供Web API中的等效解决方案吗 请在下面查找在ASP.NET MVC中实现的函数 public JsonResult FileUpload(HttpPostedFileBase file) { string extension = System.IO.Path.GetExtensi
public JsonResult FileUpload(HttpPostedFileBase file)
{
string extension = System.IO.Path.GetExtension(file.FileName);
string bufferData = string.Empty;
if (file != null)
{
using (MemoryStream ms = new MemoryStream())
{
file.InputStream.CopyTo(ms);
byte[] array = ms.GetBuffer();
var appendInfo = "data:image/" + extension + ";base64,";
bufferData = appendInfo + Convert.ToBase64String(array);
}
}
var result = new
{
Data = bufferData
};
return Json(result,"text/plain");
}
您能建议在WebAPI中使用相同的实现吗
谢谢,
Bhagatwebapi为您提供JSON,因此您可以简化端点上的代码处理。默认情况下,您需要在WebApiConfig.cs中进行更改,以使一切正常工作。我已在下面修改了您的方法:
public HttpResponseMessage FileUpload(HttpPostedFileBase file) {
var result = new HttpResponseMessage(HttpStatusCode.NotFound);
var bufferData = string.Empty;
try
{
if (file != null)
{
var extension = System.IO.Path.GetExtension(file.FileName);
using (MemoryStream ms = new MemoryStream())
{
file.InputStream.CopyTo(ms);
var array = ms.GetBuffer();
var appendInfo = "data:image/" + extension + ";base64,";
bufferData = appendInfo + Convert.ToBase64String(array);
result.StatusCode = HttpStatusCode.OK;
// Set Headers and Content here
result.Content = bufferData;
}
}
}
catch(IOException ex)
{
// Handle IO Exception
}
return result
}
您需要在WebApiConfig.cs中进行的更改可能如下所示:
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}",
defaults: null,
constraints: new { action = @"\D+" }
);
// This makes the response default into JSON instead of XML
config.Formatters.Remove(config.Formatters.XmlFormatter);
}
[HttpGet]
public HttpResponseMessage HelloWorld()
{
string result = "Hello world! Time is: " + DateTime.Now;
var resp = new HttpResponseMessage(HttpStatusCode.OK);
resp.Content = new StringContent(result, System.Text.Encoding.UTF8, "text/plain");
return resp;
}
请注意,您可以对代码进行的最快修复就是这样做,但我不建议返回字符串
public string FileUpload(HttpPostedFileBase file) {
var result = new HttpResponseMessage(HttpStatusCode.NotFound);
var bufferData = string.Empty;
if (file != null)
{
var extension = System.IO.Path.GetExtension(file.FileName);
using (MemoryStream ms = new MemoryStream())
{
file.InputStream.CopyTo(ms);
var array = ms.GetBuffer();
var appendInfo = "data:image/" + extension + ";base64,";
bufferData = appendInfo + Convert.ToBase64String(array);
return bufferData;
}
}
// If you get here and have not returned,
// something went wrong and you should return an Empty
return String.Empty;
}
祝你好运-处理文件和文件返回的方法有很多种,因此我想假设你在处理时没有特殊的返回值。你不需要创建自定义格式化程序来实现这一点。而是按如下方式返回内容:
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}",
defaults: null,
constraints: new { action = @"\D+" }
);
// This makes the response default into JSON instead of XML
config.Formatters.Remove(config.Formatters.XmlFormatter);
}
[HttpGet]
public HttpResponseMessage HelloWorld()
{
string result = "Hello world! Time is: " + DateTime.Now;
var resp = new HttpResponseMessage(HttpStatusCode.OK);
resp.Content = new StringContent(result, System.Text.Encoding.UTF8, "text/plain");
return resp;
}
这对我来说是可行的,无需使用自定义格式化程序
如果您明确希望创建输出并覆盖基于Accept标头的默认内容协商,则不希望使用Request
CreateResponse()
,因为它强制mime类型
而是显式创建一个新的HttpResponseMessage
,并手动分配内容
上面的示例使用了
StringContent
,但是有很多其他内容类可用于从各种.NET数据类型/结构返回数据。我认为您不能使用Json方法将Json作为test/plain返回。您是否尝试将文件作为base64编码的字符串返回?如果是,为什么不使用FileResult呢?