[英]Modify runtime > probing attribute web.config using C#
我在 IIS 上托管了一個應用程序,其結構如下:
IISAPP
- bin
- plugins
- scripts
- default1.aspx
- web.config
在 Plugins 目錄中,我在它自己的目錄中托管了其他幾個 ASPX 應用程序,因此結構如下所示:
IISAPP
- bin
- plugins
- MyPlugin
- bin
- scripts
- default2.aspx
- web.config
- scripts
- default1.aspx
- web.config
當我導航到http://IISAPP/時,會提供 default1.aspx 頁面,這很棒。 當我導航到http://IISAPP/plugins/MyPlugin/default2.aspx 時,該頁面未提供,它抱怨找不到 dll。 這是因為插件應用程序正在尋找 bin 文件夾頂級應用程序中的 dll。
我繼續添加<probing privatePath="plugins/MyPlugin/bin" />
標簽。 這解決了提供 default2.aspx 的問題。
問題
這些插件將使用安裝程序安裝。 我必須以某種方式更改該私有路徑以包含所有文件夾。 因此,如果我添加“MyPlugin2”,我將不得不將頂層 web.config 中的探測標記修改為<probing privatePath="plugins/MyPlugin/bin;plugins/MyPlugin/bin" />
。
我需要使用 C# 在runtime
部分的probing
元素內修改下面 web.config 中的privatePath
屬性,然后保存配置文件。 我嘗試使用配置管理器執行此操作,但找不到正確的路徑。 當它看起來正確時,它會導致 null。
我也玩過以下游戲:
var config = System.Xml.Linq.XDocument.Load(System.Web.HttpContext.Current.Server.MapPath("~/Web.config"));
var runtimeNode = config.Root.Element("runtime").Element("assemblyBinding").Element("probing").Attribute("privatePath");
在這種情況下,文檔會加載,但在我的一生中,我無法獲得該 privatepath 值來更新它並保存配置。
我也嘗試了XMLDocument
,但由於 web.config 中的第 1 行,我無法加載 xml。 我真的不想刪除它,除此之外,解決方案感覺有點草率。
更新我嘗試了以下方法:
var config = System.Xml.Linq.XDocument.Load(System.Web.HttpContext.Current.Server.MapPath("~/Web.config"));
System.Xml.Linq.XElement runtimeNode = config.Root.Element("runtime");
System.Xml.Linq.XElement assemblyBindingNode = runtimeNode.Element("assemblyBinding");
System.Xml.Linq.XElement probingNode = assemblyBindingNode.Element("probing");
System.Xml.Linq.XAttribute att = probingNode.Attribute("privatePath");
att.Value = "new value";
config.Save(System.Web.HttpContext.Current.Server.MapPath("~/Web.config.xml"));
它適用於運行時節點。 就像在運行時節點中找不到 assemblyBinding 節點一樣。
web.config
<?xml version="1.0"?> <configuration> <configSections> <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > <section name="MyProject.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </sectionGroup> </configSections> <appSettings> <add key="WebFilePath" value="D:\Web Solutions\WEB" /> </appSettings> <system.web> <compilation debug="true" targetFramework="4.6.2"/> <httpRuntime targetFramework="4.5"/> <httpModules> <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/> </httpModules> </system.web> <connectionStrings> <add name="DB" connectionString="" providerName="System.Data.SqlClient"/> </connectionStrings> <system.webServer> <handlers> <remove name="ExtensionlessUrlHandler-Integrated-4.0"/> <remove name="OPTIONSVerbHandler"/> <remove name="TRACEVerbHandler"/> <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/> </handlers> <validation validateIntegratedModeConfiguration="false"/> <modules> <remove name="ApplicationInsightsWebTracking"/> <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler"/> </modules> </system.webServer> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/> <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/> <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0"/> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/> <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/> </dependentAssembly> <probing privatePath="INeedToChangeThisValue" /> </assemblyBinding> </runtime> <system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/> <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/> </compilers> </system.codedom> </configuration>
看起來這是一個xml名稱空間問題,可以通過XmlDocument
使用它。 我唯一關心的是,通常ASP.Net會在啟動時進行程序集解析,這意味着您可能需要在設置privatePath
之后重新啟動應用程序,我已經使用過ConfigurationManager.RefreshSection("runtime")
,這可能會刷新該節並獲取工作正常。
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("~/web.config"));
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("ass", "urn:schemas-microsoft-com:asm.v1");
var probing = xmlDoc.SelectSingleNode("//runtime/ass:assemblyBinding/ass:probing", nsmgr) as XmlElement;
probing?.SetAttribute("privatePath", "bin;plugins/bin");
xmlDoc.Save(Server.MapPath("~/web.config"));
ConfigurationManager.RefreshSection("runtime");
@matt_lethargic 的推薦有用嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.