CSV只返回列名,没有数据



我正在尝试让我的插件导出umbraco中博客文章的内容,每个博客文章都有一个contentType"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,我无法理解。

提前感谢!

可能是试图写入数据的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);
        }

为什么要查询数据库而不是使用ContentService?

而且你的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);
}

话虽如此,我想你稍后会添加更多的列,对吧?否则,逗号的作用就不大了;-)

我在AddComma方法中缺少一个value.Replace

private void AddComma(string value, StringBuilder sb)
    {
        sb.Append(value.Replace(',', ' '));
        sb.Append("; ");
    }

再加上这个,你的叔叔就完蛋了!某人现在应该包含所有的价值观。

相关内容

  • 没有找到相关文章

最新更新