【问题标题】:POST /api/Messaging/ 405 (Method Not Allowed)POST /api/Messaging/ 405(不允许的方法)
【发布时间】:2015-02-10 12:31:14
【问题描述】:

我有一些角度试图发布到 Web API。 我以前有这个工作,但不断遇到 CORS 问题。

我已将其放入 global.asax

protected void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
            if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
            {
                HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
                HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
                HttpContext.Current.Response.End();
            }
        }

这是 API 控制器操作签名:

 public void Post([FromBody]Message message)

[FromUri] 和 [FromBody] 都不起作用

我试过了:

config.EnableCors(new EnableCorsAttribute("*", "*", "*"));

在 WebApiConfig.cs 中,同时删除 asax 文件中的 Application_BeginRequest 代码,我得到相同的消息: 405(不允许的方法)

和控制器:

 public class MessagingController : ApiController
    {
        private IMessagingRepository DataRepository;

        public MessagingController()
        {
            DataRepository = new DataRepositoryFactory().GetMessagingInstance();
        }

        public List<Message> Get([FromUri]MessageGetModel model)
        {
            var result = DataRepository.GetMessages(model.FromId, model.ToId);

            return result;
        }

        public Message Get(string messageId)
        {
            var result = DataRepository.GetMessage(messageId);

            return result;
        }

        public void Post([FromUri]Message message)
        {
            message.Id = ObjectId.GenerateNewId().ToString();
            DataRepository.GetDataRepository().Save(message);

            DataRepository.PostOrUpdateThread(message);

        }
}

【问题讨论】:

  • 你能发布你的控制器吗?
  • 按要求 - 控制器
  • FromUri 在 POST 上看起来很奇怪,我认为应该是 [FromBody]
  • 没关系,还是应该调用action,模型会以null的形式出现
  • 您确定型号和您发送的内容正确吗?

标签: c# asp.net angularjs asp.net-web-api cors


【解决方案1】:

1- 将 [FromUri] 更改为 [FromBody] 因为您通常通过消息正文而不是通过查询字符串发布数据,通常查询字符串用于获取请求。

2- 从 Application_BeginRequest 中删除您的代码,因为 EnableCors 就足够了,它会添加 Allow 标头,并且通过 Application_BeginRequest 的代码再次添加它们,您会在响应中遇到相同标头重复的问题。

3- 您可以将 Route 属性或 RoutePrefix 添加到您的方法/控制器中,因此对于您的 Post 方法,将其替换为如下所示:

[Route("api/Messaging")]
public void Post([FromBody]Message message)
{
    message.Id = ObjectId.GenerateNewId().ToString();
    DataRepository.GetDataRepository().Save(message);

    DataRepository.PostOrUpdateThread(message);
}

4- 使用Fiddlerpostman 进行测试,并确保以 JSON 格式或 url 编码将消息添加到请求正文中。

5- 如果您将上一点中的消息设置为 JSON 格式,请确保使用将“content-type”标头设置为“application/json”,或者将 content-type 标头设置为“application/ x-www-form-urlencoded" 如果您在前一点对消息对象进行了编码。

6- 最后,您需要确保您的请求 url 发布到 yourapilocatio/api/Messaging url。

另外,你可以看到我的回答here

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2014-05-23
    • 2018-12-20
    • 2015-11-16
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-04
    • 2013-10-01
    相关资源
    最近更新 更多