Telerik Reporting REST API不接受跨源请求

时间:2018-06-06 21:11:57

标签: asp.net-web-api telerik-reporting

我的API设置如此using System.Web.Http.Cors; using Telerik.Reporting.Cache.File; using Telerik.Reporting.Services; using Telerik.Reporting.Services.WebApi; namespace API.Controllers { [EnableCors(origins: "*", headers: "*", methods: "*")] public class ReportsController : ReportsControllerBase { static ReportServiceConfiguration configurationInstance; static ReportsController() { configurationInstance = new ReportServiceConfiguration { HostAppId = "Html5App", Storage = new FileStorage(), ReportResolver = new ReportTypeResolver(), // ReportSharingTimeout = 0, // ClientSessionTimeout = 15, }; } public ReportsController() { //Initialize the service configuration this.ReportServiceConfiguration = configurationInstance; } } }

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 } ); } }

Global.asax.cs

我的public class WebApiApplication : System.Web.HttpApplication { protected void Application_Start() { ReportsControllerConfiguration.RegisterRoutes(GlobalConfiguration.Configuration); GlobalConfiguration.Configure(WebApiConfig.Register); } }

web.config

我的<dependentAssembly> <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-5.2.6.0" newVersion="5.2.6.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-5.2.6.0" newVersion="5.2.6.0" /> </dependentAssembly> 具有推荐的绑定重定向:

api/reports/formats

据我所知,一切都设置正确,我可以拨打$("#reportViewer1").telerik_ReportViewer({ serviceUrl: "http://dev-api/api/reports", reportSource: { report: "Logic.Reports.Report1, Logic", parameters: reportParam }, }); 并查看正确的数据。当我尝试加载此报告时,出现错误。

Failed to load http://dev-api/api/reports/resources/templates/telerikReportViewerTemplate-html: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:64634' is therefore not allowed access.

显示: &#34;加载报告查看器模板时出错。 (Template = http://dev-api/api/reports/resources/templates/telerikReportViewerTemplate-html)。&#34;

在页面上

,然后显示

const browser = cordova.ThemeableBrowser;

Chrome控制台中的

。我无法弄清楚我错过了什么。

1 个答案:

答案 0 :(得分:0)

您需要在WEB API中允许CORS参阅此文档:Enable CORS

  • 通过安装Microsoft ASP.NET CORs管理NuGet软件包

  • 在System.WebSe下的web.Config中添加以下代码行

  • 在system.WebServer部分的web.config中添加以下代码行

<httpProtocol>
  <customHeaders>
    <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" />
    <add name="Access-Control-Allow-Credentials" value="true" />
  </customHeaders>
</httpProtocol>

在您的控制器中添加[EnableCors(来源:“ ”,标头:“ ”,方法:“ *”)]