我试图让我的插件从umbraco中的博客帖子中导出内容,每个博文都有一个内容类型为&umbNewsItem'。因此,我检查并测试了以下代码:
//This is my Controller
using ExportUmbracoBlogsPackage.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using System.Text;
using Umbraco.Core.Persistence;
using Umbraco.Web;
using Umbraco.Web.WebApi;
using System.Data;
using System.Data.SqlClient;
namespace ExportUmbracoBlogsPackage.App_Code
{
public class ExportAllBlogsController : UmbracoAuthorizedApiController
{
[System.Web.Http.AcceptVerbs("GET", "POST")]
public void ExportAll()
{
List<BlogPosts> BlogPostList = new List<BlogPosts>();
BlogPostList = getPostList();
string attachment = "attachment; filename= BlogPosts.csv;";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("Content-Disposition", attachment);
HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.AddHeader("Pragma", "public");
HttpContext.Current.Response.CacheControl= "private";
WriteColumnName();
foreach (BlogPosts post in BlogPostList)
{
WritePostInfo(post);
}
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
private void WritePostInfo(BlogPosts post)
{
StringBuilder sb = new StringBuilder();
AddComma(post.Content, sb);
HttpContext.Current.Response.Write(sb.ToString());
HttpContext.Current.Response.Write(Environment.NewLine);
}
private void AddComma(string value, StringBuilder sb)
{
sb.Append(',', ' ');
sb.Append(", ");
}
private void WriteColumnName()
{
string columnNames = "Content";
HttpContext.Current.Response.Write(columnNames);
HttpContext.Current.Response.Write(Environment.NewLine);
}
public List<BlogPosts> getPostList()
{
UmbracoDatabase db = ApplicationContext.DatabaseContext.Database;
var select = new Sql("SELECT * FROM cmsPropertyData pd INNER JOIN cmsPropertyType pt ON pt.id = pd.propertytypeid INNER JOIN cmsContent c ON c.nodeId = pd.contentNodeId INNER JOIN cmsContentType ct ON ct.nodeId = c.contentType WHERE ct.alias = 'umbNewsItem' AND dataNtext IS NOT NULL;");
List<BlogPosts> BlogPostList = new List<BlogPosts>();
BlogPostList = db.Fetch<BlogPosts>(select);
return BlogPostList;
}
}
}
基本上它下载了csv,但是列是空白的,没有值。有关为什么值不存储在列表中的任何建议?当我检查列表中的值是什么时,它给了我9个列表项但是所有值都为null。可能导致这种情况发生的原因是什么?
没有抛出任何异常或错误,代码正在运行,但它没有正确地从数据库中提取数据,我不确定为什么,当我在SQL SERVER Management Studio中运行相同的查询时查询工作正常。当我在我的程序中运行它时,查询返回行,因为列表返回的值为Count = 9,并且在每个索引中存储了Model中的变量Content,但是这个值为null,我可以&# 39;想出来。
提前致谢!
答案 0 :(得分:1)
可能是因为试图写入数据的StringBuilder实际上从未附加任何内容吗?
private void WritePostInfo(BlogPosts post)
{
StringBuilder sb = new StringBuilder();
AddComma(post.Content, sb);
HttpContext.Current.Response.Write(sb.ToString()); // there's nothing in "sb" except commas at this point
HttpContext.Current.Response.Write(Environment.NewLine);
}
答案 1 :(得分:1)
为什么要查询数据库而不是使用ContentService?
并且您的AddComma函数不会将实际值添加到StringBuilder对象中,可能就是这样。这是我怎么做的(AddComma函数和StringBuilder使用似乎有点不必要):
private void WritePostInfo(BlogPosts post)
{
HttpContext.Current.Response.Write(String.Format("{0}, ", post.Content));
HttpContext.Current.Response.Write(Environment.NewLine);
}
如果你想保留AddComma功能,它可能看起来像这样:
private void AddComma(string value, StringBuilder sb)
{
sb.AppendFormat("{0}, ", value);
}
话虽这么说,我假设您稍后会添加更多列,对吧?否则整个逗号都不是很有用; - )
答案 2 :(得分:0)
我在AddComma方法中错过了value.Replace
:
private void AddComma(string value, StringBuilder sb)
{
sb.Append(value.Replace(',', ' '));
sb.Append("; ");
}
加上这个并扯掉你的叔叔! sb现在应该包含所有值。