5

I have a basic Web API controller built in MVC 6 (beta 3) as part of a new ASP.NET project. The problem I’ve come across is that it doesn’t accept the verbs PUT or PATCH and returns an error 404 whenever I try to access a URL with those methods.

Here’s what I’ve got as a basic test:

namespace Test.Controllers
{
    [Route("api/test")]
    public class TestController : Controller
    {
        [HttpGet]
        public string TestGet()
        {
            return "Hello from GET!";
        }

        [HttpPost]
        public string TestPost()
        {
            return "Hello from POST!";
        }

        [HttpDelete]
        public string TestDelete()
        {
            return "Hello from DELETE!";
        }

        [HttpPut]
        public string TestPut()
        {
            return "Hello from PUT!";
        }

        [HttpPatch]
        public string TestPatch()
        {
            return "Hello from PATCH!";
        }
    }
}

Visiting http://localhost/api/test with 'Postman' to check the URL with each of the verbs (GET, POST, DELETE, PUT, and PATCH) in turn works fine for GET, POST, and DELETE, but gives a 404 with PUT and PATCH.

Edit: I remember there being a way of enabling these verbs for MVC5 and lower that involved disabling WebDAV and adding handlers for the two verbs via web.config, but as there’s no such thing as web.config in ASP.NET 5 I’m at a total loss as to how to fix this. I’m assuming it’s probably solved via config.json but all my attempts to search for this have returned nothing helpful at all!

A previous site I developed in MVC5 doesn’t have this problem, and having looked at the web.config file there doesn’t appear to be anything in there that disables WebDAV (it’s actually uninstalled) or allows handling of PUT/PATCH methods for extensionless URLs. So I don’t think that what I wrote previously applies.

Any ideas?

Thanks

Dylan Parry
  • 3,373
  • 6
  • 26
  • 38
  • 1
    Web.config still exists and required for IIS – Yishai Galatzer Mar 04 '15 at 00:21
  • 1
    Despite not changing anything in my code, it appears to be working fine this morning. I am going to put this down to being a bug somewhere as there’s no reason why it shouldn’t have worked before and suddenly started working now. – Dylan Parry Mar 04 '15 at 12:43
  • 1
    It is strange that above code just works fine for every verb for me. – dotnetstep Mar 05 '15 at 17:45
  • 1
    It seems very intermittent for me. Sometimes it works fine, but then other times it’ll fail and give me a 404 error. I’m guessing there must be a bug somewhere in either the Roslyn compiler or IIS Express as it seems that just restarting the site in IIS Express usually fixes it. It’s a minor pain, but at least there wasn’t anything wrong with my code! – Dylan Parry Mar 05 '15 at 17:56
  • 1
    Closing by author's request. – Jeremy Apr 22 '15 at 06:00

1 Answers1

2

web.config support is only removed from the .NET and ASP.net part of the app. If your application is hosted in IIS, you still need a web.config just like you did for web API.

Yishai Galatzer
  • 8,791
  • 2
  • 32
  • 41
  • 1
    Thanks. Where would I put the ```web.config``` in my project? If I put it in the root of the project, then it won’t get copied across when publishing it, but if I put it in ```wwwroot``` then won’t it either overwrite the ```web.config``` created by publishing or get overwritten by it? Or do I have to manually add the file after publishing? If so, is there a simple way of getting VS2015 (using CTP6 at the moment) to append these settings automatically so I don’t have to do it manually every time I publish my site? – Dylan Parry Mar 04 '15 at 11:14
  • I’ve edited my question as I no longer believe that the problem in related to a ```web.config``` setting as a previous project developed in MVC 5 didn’t require any special settings and worked fine with PUT/PATCH verbs. – Dylan Parry Mar 04 '15 at 12:32