سؤال

I already read through the suggestions here and here but have not been able to get this to work. I'm new to WebApi and have got it working for GET and have not been able to do a PUT.

I should first note that I'm not hosting thw api in IIS yet, just debugging locally straight from VS. Is that a problem?

Can anyone spot what I might be doing wrong or have missed? I'm using WebApi2 and have installed CORS and AttributeRouting. As suggested I was going to uninstall WebDav but looked for it and could not find it anywhere so I assume I'm OK there.

So, here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BLL = MyApp.BLL;
using MyApp.ObjectModel;
using System.Web.Http.Cors;
using AttributeRouting.Web.Http;
namespace MyApp.Web.Controllers
{

    [EnableCors("*", "*", "GET,POST,PUT,DELETE")]     
    public partial class PersonController: ApiControllerBase
    {
             private readonly BLL.Person PersonBll;

            public PersonController()
            {
                PersonBll = new BLL.Person();
            }

            // GET api/PersonBrief/5
             //[Route("api/PersonBrief/{id}")]
            public Person GetBrief(int id)
            {
                return PersonBll.GetBrief(id);
            }

            // GET api/Person/5
             //[Route("api/Person/{id}")]
            public Person Get(int id)
            {
                return PersonBll.Get(id);
            }

            // POST api/Person
            public Person Post(Person Person)
            {
                return PersonBll.Add(Person);
            }

            // PUT api/Person
            public IHttpActionResult Put(Person Person)
            {
                try
                {
                    PersonBll.Update(Person);
                    return Ok<int>(Person.PersonID);
                }
                catch (Exception ex)
                {
                    return InternalServerError(ex);
                } 
            }

             // DELETE api/Person       
            public void Delete(int id)
            {
                PersonBll.Deactivate(id);
            }
    }
}

The GET works fine but the PUT just gives this error when trying from Chrome's PostMan: "Message": "The requested resource does not support http method 'PUT'."

Here is the WebApiConfig...

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API routes
            config.MapHttpAttributeRoutes();

            // Convention-based routing.
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

            config.EnableCors();
        }
    }

If more information is needed I'm happy to update with details. My guess is I'm missing some real big key concept or something :) Any help is greatly appreciated and I will mark the correct answer.

UPDATE 1:: OK, so after playing around a lot, I was able to pinpoint when a PUT works and does not work... I've updated the Controller code above so that it includes all the details. Basically, what I found is that if I remove the AttributeRouting I placed on the Get and GetBrief methods (ex: [Route("api/PersonBrief/{id}")]), I am able to successfully POST, PUT, and DELETE into the controller! But I obviously added those attributes to the Get and GetBrief methods so that I could hit them. Without the attributes I get the ambiguous error since it doesn't know which to hit. Why are those causing the problem, and how can I get around this?

هل كانت مفيدة؟

المحلول

I was able to figure it out in case it helps anyone else. My solution was to add Attribute Routing to every method in the controller. Final result signatures looks like the following and is working great!

[EnableCors("*", "*", "GET,POST,PUT,DELETE")]
[RoutePrefix("api")]
public partial class PersonController: ApiControllerBase

~~

        // GET api/PersonBrief
        [Route("PersonBrief")]
        public IHttpActionResult GetAllBrief()

        // GET api/PersonBrief/5
        [Route("PersonBrief/{id}")]
        public IHttpActionResult GetBrief(int id)

        // GET api/Person
        [Route("Person")]
        public IHttpActionResult GetAll()

        // GET api/Person/5
        [Route("Person/{id}")]
        public IHttpActionResult Get(int id)

        // POST api/Person
        [Route("Person/")]
        public IHttpActionResult Post([FromBody]Person Person)

        // PUT api/Person
        [Route("Person/")]
        public IHttpActionResult Put([FromBody]Person Person)

        // DELETE api/Person
        [Route("Person/{id}")]      
        public IHttpActionResult Delete(int id)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top