【发布时间】:2016-08-02 10:37:19
【问题描述】:
我正在尝试在我的 ASP.Net MVC 网站中启用 CORS。我一直在使用的参考链接可以找到here。
我下载了 CORS 的 nuget 包,并将以下更改应用于我的代码。
我在 Web.Config 中添加了 CORS 模块,如下所示:
<system.webServer>
<modules>
<remove name="FormsAuthentication" />
</modules>
<!---CORS-->
<modules runAllManagedModulesForAllRequests="true">
<add name="MvcCorsHttpModule"
type="Thinktecture.IdentityModel.Http.Cors.Mvc.MvcCorsHttpModule"/>
</modules>
<!---CORS-->
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
在 global.asax.cs 中,我添加了:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
//--CORS
RegisterCors(MvcCorsConfiguration.Configuration);
}
private void RegisterCors(MvcCorsConfiguration corsConfig)
{
var corsAttr = new EnableCorsAttribute("https://www.youtube.com", "*", "*");
corsConfig.EnableCors();
}
//xxCORS
但我在使用 MvcCorsConfiguration 时遇到错误:
当前上下文中不存在名称“MvcCorsConfiguration”。
我需要添加命名空间吗?请帮忙!
【问题讨论】:
-
你使用的是什么版本的 ASP.NET MVC?
-
@AGB:我使用的是 5.2 版。如果您能找到问题所在,请提供帮助。我已经坚持了好几个小时了!我只是想从 youtube 播放视频,但它一直给出这个错误:请求的资源上没有“Access-Control-Allow-Origin”标头。因此,不允许访问 Origin 'null'。"
-
我认为您使用的教程有点过时了。你可以试试this Nuget package 并关注项目创建者的this sample 吗?
标签: c# asp.net-mvc cors same-origin-policy