我正在嘗試在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
我已經設法找到web.config文件的路徑至今:字符串absolutePath = this.Host.ResolvePath( 「../../../web.config」); – user230910
我試過你的解決方案,但它不工作,我認爲它允許查找的位置有一些限制,並且當前目錄位於C:\ ProgramFiles某處 – user230910