有没有一种方法可以使用ASP.NET API控制器返回XML文件内容?

ajsxfq5m  于 6个月前  发布在  .NET
关注(0)|答案(1)|浏览(84)

我试图模拟一个我没有访问权的服务器进行测试。服务器返回我正在测试的客户端读取并加载到XmlDocument类中的XML文件的内容。看起来读取XML文件并返回其内容应该很容易,但我似乎无法弄清楚。如果我将文件加载到XmlDocument中并返回InnerXML,控制器将结果 Package 在根目录中,而客户机上的XmlDocument拒绝它。

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/"><?xml version="1.0" encoding="utf-8"?><credential xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><user>User</user><password>Password</password><cookie>Cookie Text</cookie><site xmlns="vs">VS Site</site><funcList>func1, func2, func3</funcList></credential></string>

字符串
如果我试图返回XmlDocument,控制器会抛出一个异常:“System.Xml.XmlDocument”是一个无效的集合类型,因为它没有一个带“System. Object”类型参数的有效Add方法。

using Newtonsoft.Json;
using POSComWebTest.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Http;
using System.Xml;
using System.Xml.Serialization;

namespace WshhWebApi.Controllers
{
    [RoutePrefix("SiteController")]
    public class AuthorizeController : ApiController
    {
        [Route("cgi-bin/CGILink")]
        [HttpGet]
        public IHttpActionResult CGILink(string cmd, string user, string passwd)
        {
            try
            {
                string file = Path.Combine(HttpContext.Current.Server.MapPath("~"), "App_Data", "credential.xml");
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(file);
                HttpContext.Current.Response.ContentType = "application/xml";
                //return Content(System.Net.HttpStatusCode.OK, xmlDoc.InnerText);  // garbage
                return Content(System.Net.HttpStatusCode.OK, xmlDoc.InnerXml); // <string root> client can't read as xml
                //return Content(System.Net.HttpStatusCode.OK, xmlDoc); // 'System.Xml.XmlDocument' is an invalid collection type since it does not have a valid Add method with parameter of type 'System.Object'.
            }
            catch (Exception ex)
            {
                return BadRequest($"CGILink Failed: {ex.Message}");
            }
        }
    }
}


看起来这应该很容易,我希望我没有谷歌错误的问题。谢谢!
这是我的XML文件credential.xml:

<?xml version="1.0" encoding="utf-8"?>
<credential xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <user>User</user>
  <password>Password</password>
  <cookie>Cookie Text</cookie>
  <site xmlns="vs">VS Site</site>
  <funcList>func1, func2, func3</funcList>
</credential>

bcs8qyzn

bcs8qyzn1#

首先,我想感谢大家的回答!我想我弄明白了。如果我只是下载文件为“应用程序/八位字节流"客户端能够把它变成一个XmlDocument。我真的不认为这会工作,但我已经花了太多的时间在这方面,越来越绝望。下面是我的解决方案:

[Route("cgi-bin/CGILink")]
[HttpGet]
public IHttpActionResult CGILink(string cmd, string user, string passwd)
{
    try
    {
        string file = Path.Combine(HttpContext.Current.Server.MapPath("~"), "App_Data", "credential.xml");
        using (FileStream fileStream = File.OpenRead(file))
        {
            MemoryStream memStream = new MemoryStream();
            memStream.SetLength(fileStream.Length);
            fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
            var result = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new ByteArrayContent(memStream.GetBuffer())
            };

            result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            var response = ResponseMessage(result);

            return response;
        }
    }
    catch (Exception ex)
    {
        return BadRequest($"CGILink Failed: {ex.Message}");
    }
}

字符串
再次感谢所有回应的人。

相关问题