HttpResponseMessage.Content位于数组

时间:2018-11-15 20:02:25

标签: c# angularjs c#-4.0 httpresponse httpresponsemessage

对控制器的角度调用

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

我的C#控制器

  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;

        }
    }

}

我正在从看起来像

的WebAPI获取结果
dynamic\431d4460-e95d-4d89-a3d8-d452609632d5.map

帖子上返回的

DynamicMapFileName = client.UploadString(theURI, jSonObject);

我将返回字符串捆绑在一起,并将其返回到调用角度

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

我在Angular中获得的回报

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"

如何才能以可以使用的字符串格式取回字符串值?

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

所以即使这票获得2票,我认为这对某些人还是有帮助的。

我最终要做的是摆脱HttpResponseMessage。 我实际上创建了所谓的“ SimpleResponse”

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

}

然后我用SimpleResponse返回类型替换了HttpResponseMessage

 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;

    }
}

}

现在我得到了一个可用的字符串。