gpt4 book ai didi

c# - 不允许发布方法

转载 作者:太空宇宙 更新时间:2023-11-03 15:34:32 25 4
gpt4 key购买 nike

我试过这个例子:http://www.c-sharpcorner.com/UploadFile/surya_bg2000/developing-wcf-restful-services-with-get-and-post-methods/ .

GET 方法工作完美,但 POST 方法不是。调试时,strReturnValue 变量始终为空。当我继续时,状态是:405 Method Not Allowed。我做错了什么?

在 C# 中,我必须将方法从 POST 更改为 OPTIONS。

我正在使用 Restangular( Angular js)。这是前端函数:

        var message = {
Name: new_player.name,
Created: (new Date()).toJSON(),
Affilation: new_player.human,
auth: new_player.auth
}
return Restangular.one('').post('CreatePlayer', message).then(function(){
console.log("Object saved OK");
}, function() {
console.log("There was an error saving");
});

enter image description here

编辑

[System.ServiceModel.OperationContract]
[System.ServiceModel.Web.WebInvoke(UriTemplate = "CreatePlayer", Method = "OPTIONS", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string CreatePlayer(System.IO.Stream data);

public string CreatePlayer(System.IO.Stream data) {

//convert stream data to StreamReader
System.IO.StreamReader reader = new System.IO.StreamReader(data);

//read StreamReader data as string
string XML_string = reader.ReadToEnd();
string result = XML_string;

//return the XMLString data
return result;
}

最佳答案

必须将我的前端调用后的 header 更改为:'application/x-www-form-urlencoded'(默认的 Internet 媒体类型)。

        return Restangular.one('').customPOST({
Name: new_player.name,
Created: new Date(),
Affilation: new_player.human,
auth: new_player.fed
}, 'CreatePlayer', {},
{'Content-Type': 'application/x-www-form-urlencoded'
})

或者,您可以使用 Angular $http 服务:

$http({
url: 'http://localhost:31736/BusinessService.svc/CreatePlayer',
method: 'POST',
data: "test",
headers: {"Content-Type": "application/x-www-form-urlencoded"}
});

就是这样!然后我可以序列化生成的字符串并继续处理我的业务逻辑。

    public string CreatePlayer(System.IO.Stream data) {

//convert stream data to StreamReader
System.IO.StreamReader reader = new System.IO.StreamReader(data);

//read StreamReader data as string
string XML_string = reader.ReadToEnd();

System.Web.Script.Serialization.JavaScriptSerializer json_serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
BusinessObjects.Player Player = json_serializer.Deserialize<BusinessObjects.Player>(XML_string);

return BL_CreatePlayer.CreatePlayer(Player);
}

关于c# - 不允许发布方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32309605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com