Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove Escape characters from WebAPI

I have to work with an old product that takes strings as the response from calling outside websites. Its not JSON or XML types. Even though the data is an xml string.

The issue is that my WebAPI is sending back the escape characters for c# in the returned value. for example \"1.0\" that is the raw string returned. Is there a way to get rid of the escape characters in the returned response? This seems simple enough. Maybe an attribute on the api call.

public class DumpController : ApiController
{
    public string Get()
    {

        var myobject = new OrderPackage();
        myobject.Shipper = "something here";
        myobject.ShippingMethod = "So jones said \" this is not right \"";
        myobject.TrackingId = "19199n99fuajf";
        myobject.Id = 1;
        var result = Utility.GenericFunctions.ToXml(myobject, myobject.GetType());
        return result;

    }
}
[Serializable()]
public class OrderPackage
{
    public int Id { get; set; }
    public int OrderId { get; set; }
    public string TrackingId { get; set; }
    public Nullable<System.DateTime> PickupDate { get; set; }
    public string Shipper { get; set; }
    public string ShippingMethod { get; set; }
    public string PackageCreatorId { get; set; }
    public string CartPackageId { get; set; }
}

Raw returned string:

 "<?xml version=\"1.0\" encoding=\"utf-8\"?><q1:OrderPackage......

I need it not to print the \ character.

like image 391
Dan Avatar asked Mar 08 '23 11:03

Dan


2 Answers

In that case, you should return a custom HttpResponseMessage with a plain text content, because when you return a "string" type in your action method, the default content serializer of the framework would be used and that's why your string will be encoded as a javascript string.

[HttpGet]
public HttpResponseMessage Get()
{
    var myobject = new OrderPackage();
    myobject.Shipper = "something here";
    myobject.ShippingMethod = "So jones said \" this is not right \"";
    myobject.TrackingId = "19199n99fuajf";
    myobject.Id = 1;
    var result = Utility.GenericFunctions.ToXml(myobject, myobject.GetType());

    var response = new HttpResponseMessage(HttpStatusCode.OK);
    response.Content = new StringContent(result, System.Text.Encoding.UTF8, "text/plain");
    return response;
}

Let me know if that helped you

like image 52
Mr.Matt Avatar answered Mar 20 '23 18:03

Mr.Matt


I figured it out. This post answered it: How to return raw string with ApiController?

Simple run down. Change the return type from String to HttpResponseMessage. The reason is that the raw string return type is still keeping string in memory as c# sees it. So just like a basic method with a return type of string the webapi return type works exactly the same way.

His code that fixed my issue:

public HttpResponseMessage Get()
{
return new HttpResponseMessage()
{
    Content = new StringContent(
        "<strong>test</strong>", 
        Encoding.UTF8, 
        "text/html"
    )
};
}
like image 41
Dan Avatar answered Mar 20 '23 19:03

Dan