Developer forum

Forum » CMS - Standard features » Consuming a remote web service in cshtml

Consuming a remote web service in cshtml

Boxx
Reply

How are you supposed to call a remote web service in a Dynamicweb Template file (.cshtml)?

I need a page that calls the web service and returns JSON.

 


Replies

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi Boxx / Scott,

Nothing of that is Dynamicweb specific so just pick your mechanism of choice available in .NET. One option for server to server calls would be the HttpClient. Here's an example: https://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client

Depending on what you're doing exactly, you may want to call the service client side instead for which you could use jQuery for example.

Note that you should see Razor templates as views responsible for displaying output, not necessarily for generating or retrieving that output. Calling a web service from a template means a blocking call which could mean your pages load slower, or at the very least are perceived to load slower. Depending on what it is you're trying to do, there may be better places to call a service from (the client, a custom component on the server that uses caching for example).

Hope this helps,

Imar

 

You must be logged in to post in the forum