[Factory Configuration] How To - Add Custom Response Headers

[Factory Configuration] How To - Add Custom Response Headers

  
Forge Component
(11)
Published on 27 Sep (yesterday) by OutSystems
11 votes
Published on 27 Sep (yesterday) by OutSystems
Hi Guys,

imagine that you need to enable CORS on your application, one possible way to do it is to add headers to your response via the Factory Configuration component.

I'll guide you on how to do it, step by step.

First go to your IIS Manager, and look for your eSpace in the sites directory:




In this example I'll use the sSpace named MSA_Sandbox, right click on it and click Explore, this will open the directory containing all related eSpace files, lookup for the web.config file (it's a xml file):



Open the file and copy its contents, go to an online XSL Transformer tool, I've used freeformatter.com and paste in the XML input what you've copied from the web.config file.



Now go to http://your.server.com/FactoryConfiguration/ListSharedConfigurations.aspx and create a new shared configuration, for this example we're going to create one based on a pre-made sample called Custom HttpRuntime Parameters (.NET), after selecting it press the fill button, this will add a template to the value text area, now we're going to change it in order to obtain our desired result.

Copy this tempalte and head over to the freeformatter.com again and paste it in the XSL input.



Look for and replace the two following lines:

<xsl:attribute name="executionTimeout">180</xsl:attribute>
<xsl:attribute name="maxRequestLength">65536</xsl:attribute>

with:

<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET,POST,OPTIONS" />
<add name="Access-Control-Allow-Headers" value="*" />

since this are the headers that we're looking for to add in order to enable CORS for this example. After that we now can press the Transform XML button and see the result bellow.


<?xml version="1.0" encoding="UTF-8"?>
...
      <httpProtocol>
         <customHeaders>
            <add value="*" name="Access-Control-Allow-Origin" />
            <add value="GET,POST,OPTIONS" name="Access-Control-Allow-Methods" />
            <add value="*" name="Access-Control-Allow-Headers" />
         </customHeaders>
      </httpProtocol>
...
As you can see now we've the headers present in our output xml, so we're ready to copy the transformation and go back to the Factory Configuration paste it there, give it a name (I gave mine CORS) and save it.

What we've done in the freeformatter.com it's what the platform will do when the eSpace is published, it will read the existing web.config file, run over the tranformation to generate a new config file that will be used in runtime by the IIS.

While developing other shared configurations, please make sure they work on the xsl processor before applying to the espace. This will save you a lot of headaches trying to figure out what's wrong with your XSL.

Now that we've our shared configuration created we need to associeate it to our eSpace, go to the eSapces tab search for your eSpace



Click in the name of the eSpace, and on the next page select the shared configuration that was previosly created, from the combobox and associate it.



And now you can see that we've the shared configuration associated to the eSpace.



Now in order to make it available at runtime we need to republish our eSpace, after that we should now see the response header in the network tab of chrome inspector for example.



There we have it, now IIS is responding with the headers we've just added in the Factory Configuration.

You can learn more about:
Cheers,
Miguel