This is a migrated thread and some comments may be shown as answers.

Telerik Reporting REST API not accepting cross origin requests

1 Answer 1211 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Zach
Top achievements
Rank 1
Zach asked on 06 Jun 2018, 09:15 PM
My API has a `ReportsController` set up like so:

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

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

My `Global.asax.cs`:

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

My `web.config` has the recommended binding redirects:

    <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>

As far as I can tell, everything is set up correctly, I can call `api/reports/formats` and see the correct data.  When I try to load this report, I get an error.

$("#reportViewer1").telerik_ReportViewer({
serviceUrl: "http://dev-api/api/reports",
reportSource: {
report: "Logic.Reports.Report1, Logic",
parameters: reportParam
},
});

It displays:
"Error loading the report viewer's templates. (Template = http://dev-api/api/reports/resources/templates/telerikReportViewerTemplate-html)." 

on the page, and displays 

`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.`

in the Chrome console.  I cannot figure out what I am missing.

1 Answer, 1 is accepted

Sort by
0
Silviya
Telerik team
answered on 08 Jun 2018, 10:00 AM
Hi Zach,

I did some testing and it seems that the binding redirects are causing the issue. I added proper binding redirects for the referenced assemblies (in my case the assemblies are referring version 5.2.3.0, but you can replace it with your current version). For example:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Http" culture="neutral" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="0.0.0.0-65535.65535.65535.65535" newVersion="5.2.3.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Formatting" culture="neutral" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="0.0.0.0-65535.65535.65535.65535" newVersion="5.2.3.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

For more information, please refer to How To: Add Telerik Reporting REST Web API to Web Application (step 2).
I'm also attaching a video record with steps to enable CORS. Note that the video is silent, in .swf format that can be opened in browser with installed Flash plugin.

Regards,
Silviya
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Zach
Top achievements
Rank 1
Answers by
Silviya
Telerik team
Share this question
or