c# - 不允许发布方法

标签 c# angularjs wcf restangular

我试过这个例子: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/

相关文章:

.net - 从 WSDL 生成服务契约

c# - 如何判断我运行的是哪个 WCF 版本?

c# - 将键盘 Hook 设置为 TextBox

c# - WinForms:寻找一种简单的方法来弹出 'Processing..' 面板

c# - 如何使用 .NET 6 的新最小托管模型在集成测试中使 Serilog 静音

javascript - 让 holder.js 与 Angular 一起工作

angularjs - 将 ControllerAs 与指令一起使用

javascript - 当 Angular 元素的子元素完成链接时运行脚本

wcf - Windows 8 上使用 Microsoft 帐户的 Azure ACS 2.0

c# - WPF 设计器引用枚举问题