【发布时间】:2015-02-14 17:48:33
【问题描述】:
我尝试了这篇文章中写的所有内容:http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api,但没有任何效果。 我正在尝试使用 angularJS 从 webAPI2 (MVC5) 获取数据以在另一个域中使用。
我的控制器如下所示:
namespace tapuzWebAPI.Controllers
{
[EnableCors(origins: "http://local.tapuz.co.il", headers: "*", methods: "*", SupportsCredentials = true)]
[RoutePrefix("api/homepage")]
public class HomePageController : ApiController
{
[HttpGet]
[Route("GetMainItems")]
//[ResponseType(typeof(Product))]
public List<usp_MobileSelectTopSecondaryItemsByCategoryResult> GetMainItems()
{
HomePageDALcs dal = new HomePageDALcs();
//Three product added to display the data
//HomePagePromotedItems.Value.Add(new HomePagePromotedItem.Value.FirstOrDefault((p) => p.ID == id));
List<usp_MobileSelectTopSecondaryItemsByCategoryResult> items = dal.MobileSelectTopSecondaryItemsByCategory(3, 5);
return items;
}
}
}
【问题讨论】:
-
也分享您的角度代码以请求 cors
-
他的角度代码可能没有问题,因为大多数 CORS 问题只是因为服务器配置
-
我有同样的设置,我注意到当我在 API 上请求一个不存在的操作时,WebApi 返回一个 404,CORS 标头丢失并且浏览器会抱怨。所以,也许就这么简单。
标签: c# asp.net-mvc angularjs asp.net-web-api cors