3

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

Ranjit
  • 4,797
  • 3
  • 28
  • 62

4 Answers4

4

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.

Community
  • 1
  • 1
djikay
  • 9,792
  • 8
  • 40
  • 50
  • sorry..no solutions worked for me ..so at last i changed my method from put to post..thanks – Ranjit Jun 28 '14 at 13:05
  • Sorry to hear the above didn't help. It'd be interesting to find out why but, according to this [link](http://stackoverflow.com/questions/6058027/cassini-and-iisexpress-put-delete-verbs-cause-405-http-code), there may be nothing you can do. At least you're up and running, which is the main thing. At the end of the day, you need to be pragmatic and move on with a workaround, as you did. – djikay Jun 28 '14 at 13:14
  • If you've done the above but are still struggling with this and you're using AttributeRouting, then this fixed it for me: http://stackoverflow.com/questions/14321988/http-put-not-allowed-in-asp-net-web-api tl;dr: There's a bug in AttributeRouting where HttpPut requests are blocked unless you explicitly decorate the method with [HttpPut]. This may have been fixed in more recent versions but it could still pose an issue if you're using an older version. – Tom Oct 24 '14 at 13:58
  • I uninstalled WebDAV from my Windows 7 install and it broke all CSS/JS delivery (it started coming across as text/html and the browser rejected it). Promptly reinstalled WebDAV. So... not that I understand why but why ever WebDAV is the problem with certain verbs, uninstalling it is not necessarily a good option either. – Josh Sutterfield Sep 20 '16 at 18:32
1

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

Josh Sutterfield
  • 1,813
  • 2
  • 15
  • 24
0

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

Morten
  • 329
  • 1
  • 7
0

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

Community
  • 1
  • 1
Molibar
  • 675
  • 7
  • 20