简体   繁体   中英

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. 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)

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 Code:

            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:

    [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 . The error 405: Method not allowed for the PUT , and also DELETE , verb is a well-known issue with this environment.

All evidence seems to centre around interference by an IIS module called WebDAV . 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.

If you know you won't need WebDAV , it's probably better to fully disable it or uninstall it from 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 .

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. 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.

If you look at the response from your web server, it lists the allowed verbs. PUT is not one of them.

Edit your web.config to enable PUT for your application.

The reason this worked locally, I believe is because IIS Express and Cassini don't enforce the verb restrictions.

Scroll down to configuration example here: 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.

See my previous answer here: 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.

<?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.

[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. So here I've just removed it from WebAPI. I had no luck allowing the specific verb PUT, nor "*". WebDAV just barfs on the PUT no matter what, from everything I can see.]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM