【问题标题】:Using CORS enabled ASP.NET Web API 2 running on localhost, I could do GET and POST requests but not PUT and DELETE requests使用在 localhost 上运行启用 CORS 的 ASP.NET Web API 2,我可以执行 GET 和 POST 请求,但不能执行 PUT 和 DELETE 请求
【发布时间】:2014-07-23 07:27:24
【问题描述】:

红色的“NetworkError:...”消息来自 PUT 和 DELETE 请求。任何人都有任何直接的想法为什么会这样?我的 web.config 已经为 CORS 配置了,如下所示:

<httpProtocol>
      <customHeaders>
        <remove name="X-Powered-By" />
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
      </customHeaders>
</httpProtocol>

我的 Web API 控制器,使用实体框架和 MSSQL:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;

namespace CarsoWebApiTraining.Controllers
{
    [EnableCors("*", "*", "*")]
    public class StudentController : ApiController
    {
        GeneralEntities entity = new GeneralEntities();

        StudentController()
        {
            entity.Configuration.LazyLoadingEnabled = false;
        }

        // GET: api/Student
        public IEnumerable<Student> Get()
        {

            return entity.Students;
        }

        // GET: api/Student/5
        public Student Get(int id)
        {
            try
            {
                entity.Students.Where(p => p.StudentID == id).FirstOrDefault();
            }

            catch 
            {
            }

            return entity.Students.Where(p => p.StudentID == id).FirstOrDefault();
        }

        // POST: api/Student
        public void Post([FromBody]Student student)
        {
            entity.Students.Add(student);
            entity.SaveChanges();
        }

        // PUT: api/Student/5
        public void Put(int id, [FromBody]Student student)
        {
            Student selectedStudent = Get(id);
            selectedStudent.Name = student.Name;
            selectedStudent.ICNo = student.ICNo;

            entity.SaveChanges();
        }

        // DELETE: api/Student/5
        public void Delete(int id)
        {
            entity.Students.Remove(Get(id));
            entity.SaveChanges();

        }


    }
}

【问题讨论】:

  • 向我们展示您的 Web api 控制器
  • 您是否使用 IIS 作为 Web 服务器?如果是这样,请确保未安装 WebDAV Publishing。为此:Windows 功能 > Internet 信息服务 > 万维网服务 > 通用 HTTP 功能 > WebDAV 发布
  • @CharlesOuellet 或者删除 web.config 文件中的 WebDav 处理程序。

标签: jquery ajax asp.net-web-api cors


【解决方案1】:

这是我发现的。这个link 可能会帮助您找到原因。您可以检查您的托管环境是否允许使用这些动词。

The other common problem when using the CORS support from 
Thnktecture.IdentityModel is that the handler for .NET code (the ExtensionlessUrlHandler) 
by default only allows GET, POST, HEAD and DEBUG methods.

【讨论】:

  • 这个答案无效,因为@Geoyws 没有使用 Thinktecture.IdentityModel 的 CORS 支持,而是 System.Web.Http.Cors 的 CORS 支持。
猜你喜欢
  • 2018-03-24
  • 2016-12-02
  • 1970-01-01
  • 2013-12-06
  • 2021-11-19
  • 1970-01-01
  • 2014-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多