简体   繁体   English

WebApi(MVC 5)PUT方法不允许HTTP / 1.1 405方法

[英]HTTP/1.1 405 Method Not Allowed for WebApi(MVC 5) PUT Method

My android app needs to update some data to server for which I have written some WebApi code for update and sending data from my android app. 我的Android应用程序需要更新一些数据到服务器,我已经编写了一些WebApi代码,用于更新和从我的Android应用程序发送数据。 Both working fine when I'm testing in local server, but after uploading to global it doesn't work and gives error like:(tested both in android app and fidler) 当我在本地服务器上测试时,两者都工作正常,但在上传到全局后它不起作用并给出如下错误:(在android app和fidler中都测试过)

HTTP/1.1 405 Method Not Allowed
Allow: GET, HEAD, OPTIONS, TRACE
Content-Type: text/html
Server: Microsoft-IIS/8.0

I used simple code both in Android and C#: 我在Android和C#中都使用了简单的代码:

Android Code: Android代码:

            HttpClient client  = new DefaultHttpClient();
            HttpPut post = new HttpPut(PUT_SETTINGS_DATA);
            try {
                JSONStringer vm = new JSONStringer().object().key("UserId")
                        .value(UserId).key("IsGPSOn").value(String.valueOf(isServiceOn)).endObject();
                post.setHeader("Accept", "application/json");
                post.setHeader("Content-type", "application/json");

                StringEntity entity = new StringEntity(vm.toString());
                post.setEntity(entity);
                HttpResponse response = client.execute(post);
                BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                String vv = "";
                while((vv = br.readLine()) != null){
                    Log.v("Response Count", vv+" "+UserId);
                }

            } catch (JSONException e1) {
                e1.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

WebApi Code: WebApi代码:

    [HttpPut]
    public int Put(SettingsVM vm)
    {
        using (var db = new ApiDBContext())
        {

            string sqlQry = "Select COUNT(ID) FROM Settings WHERE UserId= '" + vm.UserId + "'";
            var count = db.Database.SqlQuery<Int32>(sqlQry).SingleOrDefault();
            if (count != 0)
            {
                string sql = "Update Settings Set IsGPSOn={1} where UserId={0}";
                count = db.Database.ExecuteSqlCommand(sql, new object[] { vm.UserId, vm.IsGPSOn });
                db.SaveChanges();
            }
            else
            {
                string sql = "INSERT INTO Settings(UserId,IsGPSOn) VALUES({0},{1})";
                count = db.Database.ExecuteSqlCommand(sql, new object[] { vm.UserId, vm.IsGPSOn });
                db.SaveChanges();
            }
            return count;

        }

    }

I already check and test all possible solutions to resolve this error.But facing the same issue. 我已经检查并测试了所有可能的解决方案以解决此错误。但是面临同样的问题。

Thanks 谢谢

As I can see from your post, you're using IIS/8.0 . 正如我从你的帖子中看到的,你正在使用IIS/8.0 The error 405: Method not allowed for the PUT , and also DELETE , verb is a well-known issue with this environment. 错误405: Method not allowed PUT ,以及DELETE ,动词是这个环境的一个众所周知的问题。

All evidence seems to centre around interference by an IIS module called WebDAV . 所有证据似乎都集中在一个名为WebDAV的IIS模块的干扰上。 You will need to either uninstall it globally or disable it locally for your project. 您需要全局卸载它或在本地​​为您的项目禁用它。 In addition, you may need to edit your applicationhost.config file in order to allow the PUT verb. 此外,您可能需要编辑applicationhost.config文件以允许PUT动词。

If you know you won't need WebDAV , it's probably better to fully disable it or uninstall it from IIS. 如果您知道不需要WebDAV ,最好完全禁用它或从IIS卸载它。 However, if you don't know if you or someone else sharing your server need it, then it's better to disable the features you don't need locally for your project in order to avoid causing problems for others. 但是,如果您不知道您或其他人共享您的服务器是否需要它,那么最好在项目中禁用本地不需要的功能,以避免给其他人带来问题。

I could either duplicate another SO Q&A here or you could just take a look at the accepted answer to this question and also the non-accepted answer to this question . 我可以在这里复制另一个SO Q&A,或者你可以看一下这个问题的接受答案以及这个问题未被接受的答案

People normally frown upon answers that are based on information contained in links but, since they are both in SO, I'm hoping they will stay active as long as your question is active. 人们通常不赞成基于链接中包含的信息的答案,但由于他们都在SO中,我希望只要你的问题是活跃的,他们就会保持活跃。 If you find any of the information useful, please consider upvoting them. 如果您发现任何有用的信息,请考虑提升它们。

Finally, this (external to SO) link also explains how to globally uninstall the WebDAV feature from the IIS Roles and Features configuration. 最后,此(SO外部) 链接还说明了如何从IIS角色和功能配置中全局卸载WebDAV功能。

If you look at the response from your web server, it lists the allowed verbs. 如果您查看来自Web服务器的响应,它会列出允许的动词。 PUT is not one of them. PUT不是其中之一。

Edit your web.config to enable PUT for your application. 编辑web.config以为您的应用程序启用PUT。

The reason this worked locally, I believe is because IIS Express and Cassini don't enforce the verb restrictions. 我认为这在本地工作的原因是因为IIS Express和Cassini不强制执行动词限制。

Scroll down to configuration example here: http://www.iis.net/configreference/system.webserver/security/requestfiltering/verbs 在此处向下滚动到配置示例: http//www.iis.net/configreference/system.webserver/security/requestfiltering/verbs

Without having seen your request I'm quite sure you send in the ID like http://www.fluff.com/api/Fluff?id=MyID , send it in like http://www.fluff.com/api/Fluff/MyID instead. 在没有看到您的请求的情况下,我非常确定您发送的身份证如http://www.fluff.com/api/Fluff?id=MyID ,请将其发送到http://www.fluff.com/api/相反, 绒毛/ MyID

See my previous answer here: https://stackoverflow.com/a/25577497/1176452 请参阅我之前的回答: https//stackoverflow.com/a/25577497/1176452

In your web.config remove WebDAV, so make sure these 2 removes there, in addition to whatever else you already have. 在你的web.config中删除WebDAV,所以除了你已经拥有的任何其他内容之外,请确保将其删除。

<?xml version="1.0" encoding="UTF-8"?>
<system.webServer>
    <modules>
      <remove name="WebDAVModule" />
    </modules>
    <handlers>
      <remove name="WebDAV" />
    </handlers>
</system.webServer>

Alternately you can do it from IIS (it'll make the web.config change) if you remove WebDav from Handler Mappings and from Modules. 或者,如果从Handler Mappings和Modules中删除WebDav,您可以从IIS(它将使web.config更改)执行此操作。

[Background: I initially tried to get rid of WebDAV from my server altogether, but this seemed to be bad news for the actual web app (as opposed to the API) which appeared to depend somehow on the WebDAV handling for CSS & JS content. [背景:我最初尝试完全从我的服务器中删除WebDAV,但这对于实际的Web应用程序(而不是API)来说似乎是个坏消息,这似乎在某种程度上依赖于WebDAV处理CSS和JS内容。 So here I've just removed it from WebAPI. 所以在这里我刚从WebAPI中删除它。 I had no luck allowing the specific verb PUT, nor "*". 我没有运气允许特定的动词PUT,也没有“*”。 WebDAV just barfs on the PUT no matter what, from everything I can see.] 无论如何,WebDAV只是PUT上的barf,从我能看到的一切。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WebAPI删除-不允许使用HTTP / 1.1 405方法 - WebAPI DELETE - HTTP/1.1 405 Method Not Allowed WEB API PUT方法无法正常工作,并且无法使用HTTP / 1.1 405方法 - WEB API PUT method is not working and getting HTTP/1.1 405 Method Not Allowed 放置,删除方法不起作用。 错误:HTTP/1.1 405 方法不允许 - Put, Delete Method is not wokring. error : HTTP/1.1 405 Method Not Allowed 405不允许调用WebAPI的方法 - 405 Method not allowed in calling to WebAPI Web API Put 请求生成 Http 405 Method Not Allowed 错误 - Web API Put Request generates an Http 405 Method Not Allowed error .netcore PUT 方法 405 Method Not Allowed - .netcore PUT method 405 Method Not Allowed WebApi Post 方法总是返回“请求的资源不支持 http 方法 &#39;GET&#39;。” 状态:405 方法不允许 - WebApi Post Methods always Returns “The requested resource does not support http method 'GET'.” Status: 405 Method Not Allowed HTTP CODE 405(不允许的方法),当我在 ASP.NET WebAPI CORS 中发送 POST 数据时 - HTTP CODE 405 (Method Not Allowed), When I sent POST data in ASP.NET WebAPI CORS Web API 2-现在允许使用方法(405)进行PUT - Web API 2 - Method now allowed(405) for PUT MVC-Web API:不允许使用405方法 - MVC-Web API: 405 method not allowed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM