找不到.net MVC 4.5`OPTIONS` 404

时间:2018-09-05 15:19:18

标签: c# asp.net cors

使用API​​调用进行一些快速测试,但是当浏览器发送404请求时,我得到了OPTIONS状态代码。

this之后,我已经在应用程序中启用了CORS:

App_Start / WebApiConfig.cs

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

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            //defaults: new { id = RouteParameter.Optional }
            defaults: new { controller = "Home", action = "Index", id = RouteParameter.Optional }
        );
    }
}

还已经在我的api控制器中创建了OPTIONS处理程序。

MyController.cs

namespace WebApplication1.Controllers
{
    [EnableCors(origins: "*",headers:"*",methods:"*")]
    public class MyController : ApiController
    {
        public HttpResponseMessage Options()
        {
            return new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
        }

        ...

        [HttpPost]
        public async Task<IHttpActionResult> upgradeTier([FromBody]JObject data) 
        { 
          ...
        }

响应标头似乎反映了cors设置正在起作用并且被允许:

Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept
Access-Control-Allow-Methods: GET, PUT, POST, DELETE, HEAD
Access-Control-Allow-Origin: *
Connection: close
Content-Length: 1245
Content-Type: text/html
Date: Wed, 05 Sep 2018 15:01:26 GMT
X-StackifyID: V1||C63467|CD1|

直接与POST发送Postman请求将返回正确的响应。

所以我不确定为什么OPTIONS请求在这里返回404

路线有问题吗?我确实将其更改为通过自定义action

0 个答案:

没有答案