简体   繁体   中英

How to change ASP.NET ConnectionString=“<%$ ConnectionStrings:MyAppConnection %>” to work from Azure .cscfg file

We have an ASP.NET (not MVC) app we're migrating to Azure. In many pages we have code like this:

<asp:SqlDataSource runat="server" ID="ResourcesDataSource" ConnectionString="<%$ ConnectionStrings:MyAppConnection %>"
    SelectCommand="usp_list_resources" SelectCommandType="StoredProcedure"></asp:SqlDataSource>

Which reads the connectionstring from the web.config. When deploying to Azure I'd like to have the connection string in the .cscfg file, so we can use the same .cspkg to deploy to different environments and only change the .cscfg. What's the Azure equivalent of using <%$ ConnectionStrings:MyAppConnection %> ?

I could go through each of my pages and set the ConnectionString property of the datasource in the Page_Init() method, but I'd really rather not... a declarative solution would be nicer, ideally one that can be implemented with Find/Replace :)

thanks!

The thing you want is not a part of standard Azure tools.

Most probably you should build your own ExpressionBuilder . There is an article on 4guysfromrolla.com "Using Expression Builders in ASP.NET" . It shows and explains core concepts of expression builders and their internals.

A quick search on your topic lead me to another article "ASP.NET Custom Expression Builder For Azure" . Hope this will help you with your problem.

In case a link above is not available here is a code:

using System.CodeDom;
using System.Web.Compilation;
using System.Web.UI;
using Microsoft.WindowsAzure.ServiceRuntime;

public class AzureSettingsExpressionBuilder : ExpressionBuilder
{
    public override CodeExpression GetCodeExpression(BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
    {
        return new CodePrimitiveExpression(RoleEnvironment.GetConfigurationSettingValue(entry.Expression));
    }
}

Registration of the expression builder in web.config :

<compilation>
  <expressionBuilders>
    <add expressionPrefix="AzureSettings" type="AzureSettingsExpressionBuilder"/>
  </expressionBuilders>
</compilation>

The updated aspx markup:

<asp:SqlDataSource runat="server" ID="ResourcesDataSource" 
    ConnectionString="<%$ AzureSettings:MyConnectionString %>"
    SelectCommand="usp_list_resources" 
    SelectCommandType="StoredProcedure">
</asp:SqlDataSource>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM