RobVious
RobVious

Reputation: 12915

Azure Publishing Profiles - what about settings other than the connection string?

I've been relying on Azure publishing profiles to transform my web.configs with the right connection string. That's working wonderfully.

I'm now in need of another change - in my dataservice javascript file, I define the endpoint for AJAX queries as follows:

    var apiEndpoint = "http://localhost:24144/api/";
    //var apiEndpoint = "http://appservice.azurewebsites.net/api/";

I currently have to remember to manually change this to the azure endpoint before publishing. Sometimes I forget and the site breaks. Is there any way to somehow change this JS property as part of the Azure publishing profile?

Upvotes: 0

Views: 169

Answers (1)

MikeWo
MikeWo

Reputation: 10975

Not directly that I'm aware of. I'm assuming that the dataservice JavaScript file you are referring to is a static JavaScript file and that you are wanting to change the URL for the AJAX endpoint at publish time. I think you have a couple of options:

The first would be to modify your build and tap into the MS Build process to check which publish settings are being used as part of this particular build and then have a Build task manual modify the JavaScript file before packaging. I don't think I'd recommend this route because it could be pretty tricky and isn't very discoverable that this is what is happening to folks who may come along behind you and have to maintain the code.

Another option, and the one I'd probably look at if you are using MVC, is to either have the dataservice JavaScript file be dynamically created when requested, or separate the variables you need to be dynamic (such as apiEndpoint) and have that dynamically generated. For example, create a controller for your dynamic script. Then in your CSHTML file (or in the master if you need this everywhere) make a reference to the controller using a script tag like this:

<script src="@Url.Content("~/DynamicScript/ApiEndpoint")" type="text/javascript"></script>
<script src="@Url.Content("~/scripts/dataservice.js")" type="text/javascript"></script>

Then your controller could do the following:

public class DynamicScriptController : Controller
{
    public JavaScriptResult ApiEndpoint()
    {
        string dynamicScript = string.Format("var apiEndpoint = '{0}';",  ConfigurationManager.AppSettings["ajaxUrl"]);
        return JavaScript(dynamicScript);
    }
}

Finally, put the URL you want to use in your web.config file and use the transforms you have been using for your other config file values. Note that if you think you'll want to change the apiEndpoint on the fly after it is deployed to Azure you'll want to use CloudConfigurationManager instead of ConfigurationManager and move the settings to the CSDef/CSCfg files.

Note that there is some controversy over using the JavaScriptResult, but I don't think the example I'm providing here touches on that. You can see some discussion about the issue at Working example for JavaScriptResult in asp.net mvc.

If you are using WebForms/ASPX then you always fall back to reading the url out of the config like I mentioned and then use asp:Literal to basically do the same thing as the controller above did except it would be included when the page was rendered.

Upvotes: 1

Related Questions