API Controller in a different domain location

Stimulsoft Reports.WEB discussion
emanuelv
Posts: 20
Joined: Fri Sep 13, 2019 5:46 pm

API Controller in a different domain location

Post by emanuelv »

Hello,

I hope you're well. I am running Stimulsoft in a .NET Core Web project and have it set up simply like this:

Code: Select all

<script src="~/api/viewer" type="text/javascript"></script>
And the Controller is simply:

Code: Select all

[Produces("application/json")]
[Route("api/viewer")]
public class ViewerController
{
  //Code here
  [HttpGet]
  public IActionResult Get()
  {
    //Code here
  }
  
  [HttpPost]
  public async Task<IActionResult> Post()
  {
    //Code here
  }
}
My question is: is it possible to host the API controller part of it in an entirely different location? For example:

Code: Select all

<script src="https://www.my_entirely_different_domain.com/api/viewer" type="text/javascript"></script>
I tried this but it appears that while interacting with the report, it still makes calls to /api/viewer/ and it assumes that the API endpoint is hosted locally in the same place as the web project.

Is there an options setting that can be set to tell Stimulsoft that the API is in an entirely different location?

Thanks!
-Emanuel Vargas
HighAley
Posts: 8430
Joined: Wed Jun 08, 2011 7:40 am
Location: Stimulsoft Office

Re: API Controller in a different domain location

Post by HighAley »

Hello, Emanuel.

We need to reproduce the issue.
Could you send us a sample project that will help us to do this?

Thank you.
emanuelv
Posts: 20
Joined: Fri Sep 13, 2019 5:46 pm

Re: API Controller in a different domain location

Post by emanuelv »

Hello,

Yes I can. I'm attaching a zip file containing two projects. In the StimulsoftWeb project, I have a normal Angular-style setup. It is basically what was generated from clicking on Publish from one of the sample Stimulsoft demo reports. The only thing I've done is remove the ViewerController.cs because this is what I want to move over to another API location. The second thing you'll notice is that in _Layout.cshtml, I changed the script tag for the api viewer from ~/api/viewer to http://localhost:64273/api/viewer.

The second project, StimulsoftAPI is a simple API project which contains the Stimulsoft API Viewer controller. If you run this project in IISExpress, it will load up in http://localhost:64273/api/viewer and you will see the normal Stimulsoft generated script.

Next, I run StimulsoftWeb in IISExpress and expect it to load the Stimulsoft script from the location I specified in _Layout.cshtml which is http://localhost:64273/api/viewer.

At first, it does load the script from there, but then every other call to /api/viewer, it seems as though the script is trying to call it from the the web project (port 54848) instead of the API project (port 64273). In the API project, I tried specifying several options including options.Server.UseRelativeUrls, options.Server.PortNumber but none seem to make a difference.

How can I make it so that the API lives in a different location than the web front end?

Thanks!
-Emanuel
Attachments
SampleApiNewLocation.zip
(1.97 MiB) Downloaded 232 times
2020-07-06 09_16_28-Clipboard.jpg
2020-07-06 09_16_28-Clipboard.jpg (109.96 KiB) Viewed 3759 times
Lech Kulikowski
Posts: 6237
Joined: Tue Mar 20, 2018 5:34 am

Re: API Controller in a different domain location

Post by Lech Kulikowski »

Hello,

Please try to change the following options on the necessary in the ngOnInit functions after this.viewer.renderHtml('viewer') method:
window["js" + this.viewer.viewerId].options.requestUrl = "https://localhost/api";
window["js" + this.viewer.viewerId].options.requestAbsoluteUrl = "https://localhost/api";
window["js" + this.viewer.viewerId].options.requestResourcesUrl = "https://localhost/api";
window["js" + this.viewer.viewerId].options.requestStylesUrl = "https://localhost/api";

Thank you.
emanuelv
Posts: 20
Joined: Fri Sep 13, 2019 5:46 pm

Re: API Controller in a different domain location

Post by emanuelv »

Hello Lech,

Thanks for the quick reply. I tried what you suggested (although I had to use (window as any) to prevent typescript errors), but it still did not appear to have any effect. It is still requesting "/api/viewer" which it tries to resolve from the relative path of the web project. Here is what the ngOnInit() looks like:

Code: Select all

ngOnInit() {
	console.log('Loading Viewer view');

	// Setting the required options on the client side
	this.options = new Stimulsoft.Viewer.StiViewerOptions();
	//this.options.height = "650px";

	this.viewer = new Stimulsoft.Viewer.StiViewer(this.options, 'StiViewer', false);

	this.viewer.renderHtml("viewer");

	(window as any)['js' + this.viewer.viewerId].options.requestUrl = 'http://localhost:64273/api/viewer';
	(window as any)['js' + this.viewer.viewerId].options.requestAbsoluteUrl = 'http://localhost:64273/api/viewer';
	(window as any)['js' + this.viewer.viewerId].options.requestResourcesUrl = 'http://localhost:64273/api/viewer';
	(window as any)['js' + this.viewer.viewerId].options.requestStylesUrl = 'http://localhost:64273/api/viewer';

	console.log((window as any)['js' + this.viewer.viewerId]);
}
The only thing I found that works is if I save the generated javascript that comes from the API into viewer.js and then edit the javascript in the Stimulsoft.Viewer.StiViewer.prototype.getParameters() function to change the following three parameters to "requestAbsoluteUrl":

Code: Select all

jsParameters.options.requestUrl = Stimulsoft.Viewer.parameters.requestAbsoluteUrl; //changed from requestUrl
jsParameters.options.requestStylesUrl = Stimulsoft.Viewer.parameters.requestAbsoluteUrl; //obsolete  //changed from requestUrl
jsParameters.options.requestResourcesUrl = Stimulsoft.Viewer.parameters.requestAbsoluteUrl; //changed from requestUrl
However, normally, I would not be able to do this since this is the script that is generated by the API and sent back to the web front end. Is there another setting that I might be missing?

Thanks so much!
-Emanuel
Lech Kulikowski
Posts: 6237
Joined: Tue Mar 20, 2018 5:34 am

Re: API Controller in a different domain location

Post by Lech Kulikowski »

Hello,

Please try to check the following way - change static parameters collection Stimulsoft.Viewer.parameters before viewer creating:

Code: Select all

ngOnInit() {
        console.log('Loading Viewer view');

        Stimulsoft.Viewer.parameters.requestUrl = "http://localhost:64273/api/viewer";
        Stimulsoft.Viewer.parameters.requestAbsoluteUrl = "http://localhost:64273/api/viewer";

.....
Thank you.
emanuelv
Posts: 20
Joined: Fri Sep 13, 2019 5:46 pm

Re: API Controller in a different domain location

Post by emanuelv »

Hi Lech,

That seems to have worked! Thank you so much!

-Emanuel
Lech Kulikowski
Posts: 6237
Joined: Tue Mar 20, 2018 5:34 am

Re: API Controller in a different domain location

Post by Lech Kulikowski »

Hello

We are always glad to help you!

Thank you.
swaraj
Posts: 7
Joined: Wed Feb 17, 2021 7:12 am

Re: API Controller in a different domain location

Post by swaraj »

I'm using stimulsoft-designer-angular. script is loaded with the qiven url. but styles and all other request are changed to localhost. please help

I'm attaching screenshot of networks when designer is loading. The first API call is getting script from the dotnet side from 'https://my-domain/designer'. The second and third are failed because the base url is not same as 'https://my-domain/designer'. It automatically calls with localhost:portNumber as 'http://localhost:9003/designer'.
when script is loading the requestAbsoluteUrl is coming as http://localhost:9003 in the response. How can i fix this issue ? please help
Capture.JPG
Capture.JPG (64.71 KiB) Viewed 3101 times
Vadim
Posts: 363
Joined: Tue Apr 23, 2013 11:23 am

Re: API Controller in a different domain location

Post by Vadim »

Hello.

Does resuest URL to server side configured in one place, as in sample:
<stimulsoft-designer-angular [requestUrl]="'http://localhost:60801/api/designer'"..
https://github.com/stimulsoft/Samples-A ... 20Designer

?
Post Reply