对控制器的角度调用
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中得到结果
dynamic431d4460-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"
如何将字符串值恢复为可以使用的字符串格式
非常感谢您的帮助!!
因此,尽管这获得了2张反对票,我认为这对一些人来说会有所帮助。。。
我最终所做的就是离开HttpResponseMessage。我实际上创建了一个我称之为"简单响应"的
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;
}
}
}
现在我的角上有一根可用的绳子。