asp.Net   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了asp.net-web-api – WebApi 2.1 PUT抛出错误415大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用WebApi PUT方法更新数据.我的代码工作正常,但突然之间我开始得到这个错误.
"message":"The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource.","Exceptionmessage":"No MediaTypeFormatter is available to read an object of type 'xemployee' from content with media type 'application/octet-stream'.","ExceptionType":"System.Net.http.UnsupportedMediaTypeException".

这是标题:
回应标题

http/1.1 415 Unsupported Media Type
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
Set-Cookie: Role=D65520F37D105E39C1A92C15CD482E378F32A769592AC7D8305285A5B9B90362F7F2F13F14E6DC220E44D26940B06B52E7460EF13184F245805AF9523D1072464F4BD06AFB4F8AEB8B7D8BF607A8922C6041A3A4C636BF3B26388E606A94FE43; expires=Tue,07-Oct-2014 09:49:56 GMT; path=/
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue,07 Oct 2014 09:19:56 GMT
Content-Length: 809

请求标题:

PUT /api/xemployees/2110481232 http/1.1
Host: guideonline.ilvestour.office
Connection: keep-alive
Content-Length: 229
Accept: application/json,text/javascript,*/*; q=0.01
Origin: http://guideonline.ilvestour.office
X-requested-With: XMLhttprequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/37.0.2062.124 Safari/537.36
Content-Type: application/json; charset=UTF-8"
Referer: http://guideonline.ilvestour.office/account
Accept-Encoding: gzip,deflate,sdch
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: .ASPXAUTH=215C424A0A023F5B42775B7A73B08FEC8CB36E7200FBA430EADF2F300A84500571F8B5EE980C3EF2913FE160978973CDBC50BDD216E16FC342EF0B566D0944ECFD901DF471DEF9F6E5D272B52F2450CC0A1FB96BCC6B3B6E7A7C07343D4DFBD66; Role=DE678EE89D7089B8CD74B202E00C53CA9AE9E4C40B506C5C4EEF56E7962F38ED86F6BFD34E5FD3A6DD6ECCCF61AF768CAB0C1D7C5F15A8638F9454B24DF3208F021EB638235420574C6420CA5A19F0B6BD07BAC303FF79612D6C1AF246563A7
request Payloadview source
{"Kod":2110481232,"Сотрудник": "Lena","Telephon": "088-6734227","password":"rimosa57","email":"samoylova-elena@mail.ru","CrWho":"OMEGA.Administrator","CrWhen":"2014-10-07T09:20:05.735Z"}

控制器代码中没有什么特别之处:

[Authorize(Roles = "Admin,User")]
        public async Task<IhttpActionResult> Putxemployee(int id,xemployee xemployeE)
        {
            if (!ModelState.IsValid)
            {
                return Badrequest(ModelStatE);
            }

            if (id != xemployee.Kod)
            {
                return Badrequest();
            }

            try
            {
                var user = db.xemployee.Find(id);
                user.Сотрудник = xemployee.Сотрудник;
                user.Telephon = xemployee.Telephon;
                user.password = xemployee.password;
                user.email = xemployee.email;
                await db.SaveChangesAsync();
            }
            catch (DbupdateConcurrencyException)
            {
                if (!xemployeeExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
            catch (DbEntityValidationException E)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    var path = "C:/error.txt";
                    using (StreamWriter sw = File.CreateText(path))
                    {
                        sw.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",eve.Entry.Entity.GetType().Name,eve.Entry.StatE);
                    }
                    using (StreamWriter sw1 = File.CreateText("C:/error1.txt"))
                        foreach (var ve in eve.ValidationErrors)
                        {
                            sw1.WriteLine("- Property: \"{0}\",Error: \"{1}\"",ve.PropertyName,ve.ErrormessagE);
                        }
                }
                throw;
            }
            return StatusCode(httpStatusCode.NoContent);
        }

与WebApiConfig相同:

public static void Register(httpConfiguration config)
        {
            config.MaphttpAttributeRoutes();
            config.Routes.MaphttpRoute(
                name: "DefaultApi",routeTemplate: "api/{Controller}/{iD}",defaults: new { id = RouteParameter.optional }
            );
            var json = config.Formatters.JsonFormatter;
            json.serializerSetTings.Culture = new CultureInfo("ru-RU");
            config.Formatters.Remove(config.Formatters.XmlFormatter);
        }

解决方法

请求的Content-Type标头中似乎有一个打字错误(最后一个)
Content-Type: application/json; charset=UTF-8"

当此标题丢失或格式错误时,服务器将默认自动使用应用程序/八位字节流,如this post所述.

大佬总结

以上是大佬教程为你收集整理的asp.net-web-api – WebApi 2.1 PUT抛出错误415全部内容,希望文章能够帮你解决asp.net-web-api – WebApi 2.1 PUT抛出错误415所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。