微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 不支持的媒体类型web api帖子

我正在使用 angularjs发布创建webapi的数据.它在我的本地系统上工作,但是当我上传到服务器上时它无法正常工作并返回错误,不支持媒体类型.请帮我解决这个问题.

<script>
var app = angular.module('MyApp',[]);

app.controller("QueryControllerController",function ($scope,$http) {

    $scope.submit = function () {
        if ($scope.QueryDescription) {
            var product = {
                "QueryDescription": $scope.QueryDescription,"CategoryID": $scope.CategoryID

            }
            $http.post('/api/querycontroller',JSON.stringify(product)).
            success(function (data,status,headers,config) {
                alert('Added Successfully' + headers);
                $('#formusers')[0].reset();
            }).
            error(function (data,config) {
                alert(status + "," + data + "," + headers + "," + config);
            });
        }
    };

}
);

我的api控制器代码是:

[ResponseType(typeof(Query))]

    [HttpPost]
    public HttpResponseMessage Post([FromBody]Query Services)
    {

        Services.CommunityID = UserStatus.GetUserID(User.Identity.Name);
        Services = repository.Add(Services);
        var response = Request.CreateResponse<Query>(HttpStatusCode.Created,Services);
        string uri = Url.Route(null,new { id = Services.QueryID });
        response.Headers.Location = new Uri(Request.RequestUri,uri);

        return response;

    }

当我尝试发布时,我得到不支持媒体类型响应:

"Message": "The request entity's media type 'text/plain' is not supported   for this resource."

解决方法

删除[ResponseType(typeof(Query))]并使用viewmodel类作为参数

[HttpPost]
public HttpResponseMessage Post([FromBody]Productviewmodel product)
{

也…

尝试https://stackoverflow.com/users/1267724/john建议的方法(在agunlar控制器中)作为这篇文章的答案:
change Content-type to “application/json” POST method,RESTful API

Posting a JSON object is quite easy in Angular. All you need to do is the following:

Create a Javascript Object

I’ll use your exact properties from your code.

var postObject = new Object();
postObject.userId = "testAgent2";
postObject.token = "testAgent2";
postObject.terminalInfo = "test2";
postObject.forceLogin = "false";
Post the object to the API

To post an object to an API you merely need a simple $http.post
function. See below:

$http.post("/path/to/api/",postObject).success(function(data){
    //Callback function here.
    //"data" is the response from the server.
});

Since JSON is the default method of posting to an API,there’s no need
to reset that. See this link on $http shortcuts for more @R_677_4045@ion.

With regards to your code specifically,try changing your save method to include this simple post method.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐