如何使用html和xml返回多部分内容

时间:2019-06-18 17:17:20

标签: asp.net-core asp.net-web-api asp.net-core-webapi

我必须在.NET Core 2.2中重新编写Web服务,并且该服务必须返回包含XML和base-64编码的HTML的多部分响应。当前服务是一个非常老的Java程序,没有文档。

该请求来自无法更改的旧服务,并且必须接收HTML 4.01作为响应。实际示例如下所示。

  1. 我将返回哪种响应对象?
  2. 如何将XML和编码的HTML组合到一个响应对象中?

我不希望有人为我编写我的代码,但希望能对您有所帮助。

样本响应

--75df5969-8400-11d5-c000-0021ffffff97
Content-type: text/xml; charset="UTF-8"
Content-id: XMLROOT

<?xml version="1.0" encoding="UTF-8"?><XMLROOT><Service><StatusCd>0</StatusCd></Service></XMLROOT>
--75df5969-8400-11d5-c000-0021ffffff97
Content-type: application/html
Content-id: Inquiry.html
Content-description: Response to View Inquiry

PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMDEgVHJhbnNpdGlvbmFsLy9FTiI+Cgo8SFRNTD4KPEhFQUQ+CjxNRVRBIGh0dHAtZXF1aXY9IkNvbnRlbnQtVHlwZSIgY29udGVudD0idGV4dC9odG1sOyBjaGFyc2V0PUlTTy04ODU5LTEiPgo8TUVUQSBodHRwLWVxdWl2PSJDb250ZW50LVN0eWxlLVR5cGUiIGNvbnRlbnQ9InRleHQvY3NzIj4KCgk8U1RZTEUgVFlQRT0idGV4dC9jc3MiPgoJCVRSIHsgYmFja2dyb3VuZC1jb2xvcjogbGlnaHRncmV5IH0KCQkudGREYXRhIHsKCSAJCWZvbnQtc2l6ZTo4cHQ7CgkJCWNvbG9yOiMwMDAwOUM7CgkJCWZvbnQtZmFtaWx5OlZlcmRhbmEsIEFyaWFsLCBIZWx2ZXRpY2EsIHNhbnMtc2VyaWY7IH0KCQkudGRMYWJlbCB7CgkgCQlmb250LXNpemU6OHB0OwoJCQlmb250LXdlaWdodDpib2xkZXI7CgkJCWZvbnQtZmFtaWx5OlZlcmRhbmEsIEFyaWFsLCBIZWx2ZXRpY2EsIHNhbnMtc2VyaWY7IH0KCQkudGRIZWFkIHsKCQkJYmFja2dyb3VuZC1jb2xvcjojODA4MDgwOwoJCQl0ZXh0LWFsaWduOmNlbnRlcjsKCSAJCWZvbnQtc2l6ZTo4cHQ7CgkJCWZvbnQtd2VpZ2h0OmJvbGRlcjsKCQkJZm9udC1mYW1pbHk6VmVyZGFuYSwgQXJpYWwsIEhlbHZldGljYSwgc2Fucy1zZXJpZjsgfQoJPC9TVFlMRT4KCjxUSVRMRT5JbnF1aXJ5PC9USVRMRT4KPC9IRUFEPgo8Qk9EWT4KPHRhYmxlIEJPUkRFUj0iMCIgQ0VMTFBBRERJTkc9IjAiIENFTExTUEFDSU5HPSIwIiBXSURUSD0iNzAwIj4KCTx0cj4gCgkJPHRkIEJHQ09MT1I9IiNmZmZmZmYiPgoJCQk8VEFCTEUgQk9SREVSPSIxIiBDRUxMUEFERElORz0iMiIgQ0VMTFNQQUNJTkc9IjEiIFdJRFRIPSI3MDAiPgoJCQkJPFRSPjxURCBDT0xTUEFOPSI0IiBBTElHTj0iY2VudGVyIiBCR0NPTE9SPSIjZmZmZmZmIj4KCQkJCQlUZXN0IG1lc3NhZ2UgCgkJCQk8L1REPjwvVFI+CgkJCTwvVEFCTEU+CQoJCTx0ZD48L3RkPgoJPHRyPjwvdHI+CjwvdGFibGU+Cgo8L0JPRFk+CjwvSFRNTD4=
--75df5969-8400-11d5-c000-0021ffffff97--

1 个答案:

答案 0 :(得分:1)

  

我返回哪种响应对象?如何将XML和编码的HTML组合到一个响应对象中?

没有这样的内置结果类型。但是,创建包含XML + HTML(已编码)的自定义MultipartResult很容易。例如:

public class XmlAndEncodedHtmlMultipartResult : IActionResult
{
    private StringContent _xmlContent;
    private StringContent _htmlContent;
    private MultipartContent _multipartContent=new MultipartContent(Subtype); // the final content
    public static string Subtype = "my-xml+html" ;                            // `content-type: multipart/{Subtype}`

    public XmlAndEncodedHtmlMultipartResult SetXmlContent(string xml, string contentId = "XMLROOT")
    {
        var xmlContent = new StringContent(xml, Encoding.UTF8, "text/xml");
        xmlContent.Headers.Add("Content-id", contentId);
        this._xmlContent = xmlContent;
        return this;
    }

    public XmlAndEncodedHtmlMultipartResult SetEncodedHtmlContent(string rawHtml,string contentId="Inquiry.html", string description="Response to View Inquiry")
    {
        var bytes= Encoding.UTF8.GetBytes(rawHtml); 
        var encodedHtml = Convert.ToBase64String(bytes);
        var htmlContent = new StringContent(encodedHtml, Encoding.UTF8, "application/html");
        htmlContent.Headers.Add("Content-id", contentId);
        htmlContent.Headers.Add("Content-description",description);
        this._htmlContent = htmlContent;
        return this;
    }

    public async Task ExecuteResultAsync(ActionContext context)
    {
        if(this._htmlContent ==null || this._xmlContent ==null){ throw new Exception("html content & xml must be null");}
        this._multipartContent.Add(this._xmlContent);
        this._multipartContent.Add(this._htmlContent);
        var response = context.HttpContext.Response;
        response.ContentType = this._multipartContent.Headers.ContentType.ToString();
        // ... custom other headers as you like
        await _multipartContent.CopyToAsync(response.Body);
    }
}

在您的操作中,只需按如下所示进行调用即可:

public IActionResult Privacy()
{
    var xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><XMLROOT><Service><StatusCd>0</StatusCd></Service></XMLROOT>";
    var html = "<html><head></head><body>hello,world</body></html>";
    return new XmlAndEncodedHtmlMultipartResult().SetEncodedHtmlContent(html).SetXmlContent(xml);
}