Asp.net web api 从Web API下载csv文件不会启动
我有来自不同来源的示例代码,我想先试用一下,然后再将其应用到我的代码中,这与我得到的示例非常相似 我的控制器中有以下代码:Asp.net web api 从Web API下载csv文件不会启动,asp.net-web-api,export-to-csv,httpresponsemessage,Asp.net Web Api,Export To Csv,Httpresponsemessage,我有来自不同来源的示例代码,我想先试用一下,然后再将其应用到我的代码中,这与我得到的示例非常相似 我的控制器中有以下代码: [Route("getcsv")] [HttpGet] public HttpResponseMessage GetCSV() { var data = new[]{ //Suppose you filter out these data new{ Name="Ram", E
[Route("getcsv")]
[HttpGet]
public HttpResponseMessage GetCSV()
{
var data = new[]{ //Suppose you filter out these data
new{ Name="Ram", Email="ram@techbrij.com", Phone="111-222-3333" },
new{ Name="Shyam", Email="shyam@techbrij.com", Phone="159-222-1596" },
new{ Name="Mohan", Email="mohan@techbrij.com", Phone="456-222-4569" },
new{ Name="Sohan", Email="sohan@techbrij.com", Phone="789-456-3333" },
new{ Name="Karan", Email="karan@techbrij.com", Phone="111-222-1234" },
new{ Name="Brij", Email="brij@techbrij.com", Phone="111-222-3333" }
};
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StringContent(WriteTsv(data));
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); //attachment will force download
result.Content.Headers.ContentDisposition.FileName = "RecordExport.csv";
return result;
}
public string WriteTsv<T>(IEnumerable<T> data)
{
StringBuilder output = new StringBuilder();
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));
foreach (PropertyDescriptor prop in props)
{
output.Append(prop.DisplayName); // header
output.Append("\t");
}
output.AppendLine();
foreach (T item in data)
{
foreach (PropertyDescriptor prop in props)
{
output.Append(prop.Converter.ConvertToString(
prop.GetValue(item)));
output.Append("\t");
}
output.AppendLine();
}
return output.ToString();
}
[路由(“getcsv”)]
[HttpGet]
公共HttpResponseMessage GetCSV()
{
var data=new[]{//假设您过滤掉这些数据
新的{Name=“Ram”,电子邮件=”ram@techbrij.com,Phone=“111-222-3333”},
新的{Name=“Shyam”,电子邮件=”shyam@techbrij.com,Phone=“159-222-1596”},
新的{Name=“Mohan”,电子邮件=”mohan@techbrij.com,Phone=“456-222-4569”},
新的{Name=“Sohan”,电子邮件=”sohan@techbrij.com,Phone=“789-456-3333”},
新的{Name=“Karan”,电子邮件=”karan@techbrij.com,Phone=“111-222-1234”},
新的{Name=“Brij”,电子邮件=”brij@techbrij.com“,Phone=“111-222-3333”}
};
HttpResponseMessage结果=新的HttpResponseMessage(HttpStatusCode.OK);
result.Content=newstringcontent(WriteTsv(数据));
result.Content.Headers.ContentType=新的MediaTypeHeaderValue(“应用程序/八位字节流”);
result.Content.Headers.ContentDisposition=新的ContentDispositionHeaderValue(“附件”);//附件将强制下载
result.Content.Headers.ContentDisposition.FileName=“RecordExport.csv”;
返回结果;
}
公共字符串WriteTsv(IEnumerable数据)
{
StringBuilder输出=新的StringBuilder();
PropertyDescriptorCollection props=TypeDescriptor.GetProperties(typeof(T));
foreach(PropertyDescriptor道具中的道具)
{
output.Append(prop.DisplayName);//头
output.Append(“\t”);
}
AppendLine();
foreach(数据中的T项)
{
foreach(PropertyDescriptor道具中的道具)
{
output.Append(prop.Converter.ConvertToString(
道具价值(项目));
output.Append(“\t”);
}
AppendLine();
}
返回output.ToString();
}
我通过以下方式调用此方法:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function save() {
window.open('https://localhost:44370/api/values/getcsv', '_blank', '');
}
</script>
</head>
<body>
<a class="btn btn-primary" onclick="save()">Export</a>
</body>
</html>
函数save(){
打开窗户https://localhost:44370/api/values/getcsv","空白","空白",;
}
我不知道我错过了什么。请帮忙!谢谢大家! 您的Web API版本很可能使用了错误的语法
如果使用Asp.Net核心Web API,则需要将其重构为
[Route("api/[controller]")]
public class ValuesController: Controller {
//GET api/values/getcsv
[Route("getcsv")]
[HttpGet]
public IActionResult GetCSV() {
var data = new[]{ //Suppose you filter out these data
new{ Name="Ram", Email="ram@techbrij.com", Phone="111-222-3333" },
new{ Name="Shyam", Email="shyam@techbrij.com", Phone="159-222-1596" },
new{ Name="Mohan", Email="mohan@techbrij.com", Phone="456-222-4569" },
new{ Name="Sohan", Email="sohan@techbrij.com", Phone="789-456-3333" },
new{ Name="Karan", Email="karan@techbrij.com", Phone="111-222-1234" },
new{ Name="Brij", Email="brij@techbrij.com", Phone="111-222-3333" }
};
var bytes = System.Text.Encoding.UTF8.GetBytes(WriteTsv(data));
return File(bytes, "application/octet-stream", "RecordExport.csv");
}
//...code omitted for brevity
}
在Asp.Net中,CoreHttpResponseMessage
不再被视为管道的一级公民,而是像普通模型一样被序列化。您使用的是什么版本的web api?如果是核心,那就是问题所在。HttpResponseMessage不再是管道的一级公民,正在像普通模型一样进行序列化。非常感谢。这解决了我的问题!是的,我正在使用.NetCore2.2。