简体   繁体   English

如何更改ASP.NET ConnectionString =“<%$ ConnectionStrings:MyAppConnection%>”从Azure .cscfg文件工作

[英]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. 我们有一个ASP.NET(而不是MVC)应用程序,我们正在迁移到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. 其中从web.config读取connectionstring。 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. 部署到Azure时,我希望在.cscfg文件中包含连接字符串,因此我们可以使用相同的.cspkg部署到不同的环境,只更改.cscfg。 What's the Azure equivalent of using <%$ ConnectionStrings:MyAppConnection %> ? 什么是Azure相当于使用<%$ 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 :) 我可以遍历每个页面并在Page_Init()方法中设置数据源的ConnectionString属性,但我真的不愿意......声明性解决方案会更好,理想情况下可以通过查找/替换实现:)

thanks! 谢谢!

The thing you want is not a part of standard Azure tools. 您想要的不是标准Azure工具的一部分。

Most probably you should build your own ExpressionBuilder . 最有可能你应该构建自己的ExpressionBuilder There is an article on 4guysfromrolla.com "Using Expression Builders in ASP.NET" . 有一篇关于4guysfromrolla.com “在ASP.NET中使用Expression Builders”的文章 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" . 快速搜索您的主题会引导我阅读另一篇文章“ASP.NET自定义表达式构建器的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 : web.config注册表达式构建器:

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

The updated aspx markup: 更新的aspx标记:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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