2017-09-26 80 views
0

我正在嘗試在web.config文件中提供構建系統在編譯我的Web項目中的.tt文件之前更新的URL 。如何從.tt文件(T4模板)從web.config中讀取appSetting

從我.TT文件中的相關章節低於:

<#@ template debug="true" hostSpecific="true" language="C#" #> 
<#@ output extension=".ts" #> 
<#@ Assembly Name="System.Core.dll" #> 
<#@ Assembly Name="System.Configuration.dll" #> 
<#@ import namespace="System" #> 
<#@ import namespace="System.Reflection" #> 
<#@ import namespace="System.Collections.Generic" #> 
<#@ import namespace="System.Text" #> 
<#@ import namespace="System.Linq" #> 
<#@ import namespace="System.Configuration" #> 
<#@ import namespace="System.Collections.Specialized" #> 

import { Injectable } from "@angular/core"; 

<#= RegisterNameSpace("Server.Web.Dashboard.Models.APIRequest") #> 
<#= RegisterNameSpace("Server.Web.Dashboard.Models.APIResponse") #> 
<#= RegisterNameSpace("Server.Web.Dashboard.Models.APIGeneric") #> 
<#= RegisterNameSpace("Server.Data.POCODataObjects") #> 

@Injectable() 
export class WebServiceCommunicatorService { 

    rootUrl : string = '<#= ConfigurationManager.AppSettings["ServerServiceURL"] #>'; 

..... 

當我嘗試生成從.TT文件,我得到以下錯誤消息的.ts文件:

Severity Code Description Project File Line Suppression State 
Error  An expression block evaluated as Null 
System.ArgumentNullException: Value cannot be null. 
Parameter name: objectToConvert 
    at Microsoft.VisualStudio.TextTemplating.ToStringHelper.ToStringWithCulture(Object objectToConvert) 
    at Microsoft.VisualStudio.TextTemplatingF553823B88CD6076D80EB08F6EA809752D0E5DC3E61C0FA53FB2F9AC22ACABF3B7BB9C8801A54E5DBC2A556C03FA42F2EA2AFCE58B708BEC94B0968193987868.GeneratedTextTransformation.TransformText() in webservicecommunicator.service.tt:line 35 Dashboard webservicecommunicator.service.tt 35 

回答

1

您可以嘗試使用ConfigurationManager.OpenExeConfiguration(path)方法打開硬編碼路徑(不確定您是否可以執行相對路徑)。此時您可以使用返回的Configuration對象自己的AppSettings屬性。

+0

我已經設法找到web.config文件的路徑至今:字符串absolutePath = this.Host.ResolvePath( 「../../../web.config」); – user230910

+0

我試過你的解決方案,但它不工作,我認爲它允許查找的位置有一些限制,並且當前目錄位於C:\ ProgramFiles某處 – user230910

2

我假裝該文件是XML文件而不是配置文件的方法。首先我找到web.config的絕對路徑,然後加載它並讀取節點。

首先依賴關係:

<#@ Assembly Name="System.Web.dll" #> 
<#@ Assembly Name="System.Configuration.dll" #> 
<#@ assembly name="System.Xml" #> 

<#@ import namespace="System.Configuration" #> 
<#@ import namespace="System.Web.Configuration" #> 
<#@ import namespace="System.Collections.Specialized" #> 
<#@ import namespace="System.Xml" #> 
<#@ import namespace="System.IO" #> 

因此,這裏是我的嘗試:

string GetWebServiceUrl() { 
    XmlDocument doc = new XmlDocument(); 
    string absolutePath = this.Host.ResolvePath("../../../web.config");     
    doc.Load(absolutePath); 
    XmlNode node = doc.DocumentElement.SelectSingleNode("/configuration/appSettings/add[@key='ServerServiceURL']"); 
    return node.Attributes["value"].Value.ToString(); 
} 
+0

我在考慮2個答案的組合可能是正確的解決方案? – user230910

+1

有時無論什麼作品可能是正確的解決方案:) – Reddog

+0

是的,當我打開它作爲配置文件時,由於訪問保護級別,我無法訪問它。但XmlDocument的東西奏效。 – mattnificent

相關問題