带有CORS和AttributeRouting的ASP.NET Web API2中不允许HTTP PUT

问题描述 投票:3回答:1

我已经阅读了这里这里的建议,但未能使其工作。 我是WebApi的新手,并且已经让它为GET工作并且无法进行PUT。

我应该首先注意到我还没有在IIS中托管api api,只是直接从VS调试本地。 那是问题吗?

谁能发现我可能做错了什么或错过了什么? 我正在使用WebApi2并安装了CORS和AttributeRouting。 正如我所建议的那样,我要卸载WebDav,但是找不到它而且无法在任何地方找到它,所以我认为我在那里很好。

所以,这是代码:

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);
            }
    }
}

GET工作正常,但PUT只是在尝试使用Chrome的PostMan时出现此错误:“消息”:“请求的资源不支持http方法'PUT'。”

这是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();
        }
    }

如果需要更多信息,我很乐意更新详细信息。 我的猜测是我错过了一些真正的大关键概念或者其他东西:)非常感谢任何帮助,我将标记正确的答案。

更新1 ::好的,所以在玩了很多之后,我能够确定PUT何时工作并且不起作用...我已经更新了上面的控制器代码,以便它包含所有细节。 基本上,我发现如果我删除了我放在Get和GetBrief方法上的AttributeRouting(例如: [Route("api/PersonBrief/{id}")] ),我能够成功地POST,PUT和DELETE进入控制器! 但我显然将这些属性添加到Get和GetBrief方法中,以便我可以点击它们。 如果没有属性,我会得到模糊的错误,因为它不知道要打哪个。 为什么导致这个问题的人,我怎么能解决这个问题呢?

rest url-routing cors asp.net-web-api2
1个回答
3
投票

我能够弄清楚它是否有助于其他任何人。 我的解决方案是为控制器中的每个方法添加属性路由。 最终结果签名如下所示,并且工作得很好!

[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)
© www.soinside.com 2019 - 2024. All rights reserved.