启用跨域ASP.net的Web API [英] Enabling Cross Domain ASP.net Web API

查看:671
本文介绍了启用跨域ASP.net的Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对微软的Visual Studio前preSS 2013的Web版本12.0.30501.00更新2.我有2项目在里面,一个是网页API的解决方案,第二个是一个MVC项目旨在为观点。我创建一个简单的Web API返回HTT presponsemessage,我可以在提琴手去,我会看到JSON响应。然而,在我的第二个项目试图通过AJAX调用时,我收到一个跨域错误。

I have Microsoft Visual Studio Express 2013 for Web, Version 12.0.30501.00 Update 2. I have a solution with 2 project in it, one is a web api, and the second is an MVC project intended for for the views. I have create a simple web api returning httpresponsemessage and I can go in fiddler and I will see the json response. However in my second project when attempting to call through ajax I received a cross domain error.

我尝试按照该解决方案是这样的<一个href=\"http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api\">http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api.但是安装时安装封装Microsoft.AspNet.WebApi.Cors - - 项目的WebService 在控制台经理,所以我用这个行,而不是我收到了一条错误pre。 安装封装Microsoft.AspNet.WebApi.Cors 然后我尝试下一个步骤是 config.EnableCors(); 其状态的

The solution I try to follow is this http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api. However when installing Install-Package Microsoft.AspNet.WebApi.Cors -pre -project WebService in the console manager I received an error so I used this line instead. Install-Package Microsoft.AspNet.WebApi.Cors then I try to the next step which is config.EnableCors(); its state that

'System.Web.Http.HttpConfiguration' does not contain a definition for 'EnableCors' and no extension method 'EnableCors' accepting a first argument of type 'System.Web.Http.HttpConfiguration' could be found (are you missing a using directive or an assembly reference?)

我不知道什么之后,我也尝试使用System.Web.Http.Cors添加; ,但状态的命名空间不存在。这种视觉工作室被下载约3个星期前,我觉得这个功能不应该是硬盘安装,因为它完全支持,有没有我缺少的东西吗?

I am not sure what to afterward, I also try to add using System.Web.Http.Cors; but state that the namespace does not exists. This visual studio was downloaded about 3 weeks ago, I feel this feature should not be hard to install as it fully supported, is there something that i am missing?

推荐答案

您应该安装的NuGet包 Microsoft.AspNet.WebApi.Cors

You should install a Nuget package Microsoft.AspNet.WebApi.Cors

从Visual Studio中的菜单,进入管理包,然后在包的名称中键入

From the menu in visual studio, go to Management Packages, and then type in the name of the package

这篇关于启用跨域ASP.net的Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆