HttpResponseMessage.Content is in array

1.7k views Asked by At

Angular call to controller

var objData = {data: JSON.stringify(JSONObject)}
var mapFile = $scope.service.common.WriteDynamicMapFile.save({}, 
objData).$promise;
mapFile.then(function (response) {**Do Stuff**});

My C# controller

  public class WriteDynamicFileController : ApiController
{
    [System.Web.Http.AcceptVerbs("GET", "POST")]
    [System.Web.Http.HttpPost]
    public HttpResponseMessage POST([FromBody] SimpleRequest data)
    {
        String DynamicFileName = "";
        HttpResponseMessage response = new HttpResponseMessage();
        using (HostingEnvironment.Impersonate())
        {

            try
            {                  


                client.Headers.Add("X-Current-User", base.User.GetUsername(true).ToString());
                client.Headers[HttpRequestHeader.ContentType] = "application/json";
                client.UseDefaultCredentials = true;


                DynamicFileName = client.UploadString(theURI, data);

                response = new HttpResponseMessage(HttpStatusCode.OK);
                response.Content = new StringContent(DynamicFileName);






            }
            catch (Exception ex)
            {                    
                response.ReasonPhrase = ex.InnerException.ToString();
                return response
            }



            return response;

        }
    }

}

I'm getting the result back from an WebAPI that looks like

dynamic\431d4460-e95d-4d89-a3d8-d452609632d5.map

which is returned on a post

DynamicMapFileName = client.UploadString(theURI, jSonObject);

I bundle the return string up and return it to the calling angular

response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StringContent(DynamicMapFileName);
return response

What I get back in Angular looks like this

0: "d"
1: "y"
2: "n"
3: "a"
4: "m"
5: "i"
6: "c"
7: "\"
8: "4"
9: "3"
10: "1"
11: "d"
12: "4"
13: "4"
14: "6"
15: "0"
16: "-"
17: "e"
18: "9"
19: "5"
20: "d"
21: "-"
22: "4"
23: "d"
24: "8"
25: "9"
26: "-"
27: "a"
28: "3"
29: "d"
30: "8"
31: "-"
32: "d"
33: "4"
34: "5"
35: "2"
36: "6"
37: "0"
38: "9"
39: "6"
40: "3"
41: "2"
42: "d"
43: "5"
44: "."
45: "m"
46: "a"
47: "p"

How can I just get the string value back in a string format I can use?

Any help is greatly appreciated!!

1

There are 1 answers

0
Funn_Bobby On BEST ANSWER

So even though this got 2 down votes I think it will be helpful for some...

What I ended up doing was going away from the HttpResponseMessage. I actually created what I called a "SimpleResponse"

  public class SimpleResponse {
    public string data {
        get;
        set;
    }

}

Then I replaced the HttpResponseMessage with the SimpleResponse return type

 public class WriteDynamicFileController : ApiController
{
[System.Web.Http.AcceptVerbs("GET", "POST")]
[System.Web.Http.HttpPost]
public SimpleResponse POST([FromBody] SimpleRequest data)
{
    String DynamicFileName = "";
    HttpResponseMessage response = new HttpResponseMessage();
    using (HostingEnvironment.Impersonate())
    {

        try
        {                  


            client.Headers.Add("X-Current-User", 
            base.User.GetUsername(true).ToString());
            client.Headers[HttpRequestHeader.ContentType] = "application/json";
            client.UseDefaultCredentials = true;


            DynamicFileName = client.UploadString(theURI, data);

            response.data = DynamicFileName;






        }
        catch (Exception ex)
        {                    
            response.ReasonPhrase = ex.InnerException.ToString();
            return response
        }



        return response;

    }
}

}

Now I get a usable string in my angular.