通过ServerManager更新Web.config-无法写入配置文件 - 显示错误的路径。

发布于 2025-02-09 04:43:05 字数 1170 浏览 0 评论 0原文

作为安装程序代码的一部分,我们正在尝试更改IIS(检查和创建虚拟目录,然后在其web.config文件中添加一个部分。

安装程序工作正常,直到最近,一个奇怪的错误开始困扰着我们并阻止了安装程序从程序

错误说/core/service/userVice失败

。 “默认网站/核心/服务/用户服务”是一个虚拟目录 IIS中的“默认网站/核心”虚拟目录。

“默认网站/核心”具有C的物理路径:\ weblity1 \ core \ common,而“默认网站/core/core/service/uservice”是用“ C:\ werfeers1 \ core core \ service \ service \ uservice”

而不是 物理路径创建的。当然,为什么错误指向 web.config 的错误文件夹路径。

我们使用的代码就是这样,

using (ServerManager mgr = new ServerManager())
{
  //code to add virtual path to root website (default web site)
  mgr.CommitChanges();
}
using (ServerManager mgrpolicy = new ServerManager())
{
  Configuration config = mgrpolicy.GetWebConfiguration("Default Web Site","/Core/Service/UserService");
ConfigurationSection hndlesection = config.GetSection("system.webserver/handlers");
hndlesection["accessPolicy"] = "Read,Write"; //this value is dynamic based on installer input but hardcoded here for your reference.

  mgrpolicy.CommitChanges(); //issue comes after this line
}

我们正在.NET 4.8 Windows应用程序上运行此安装程序,其中以上代码作为指向namespace System.web.administration的类库的一部分运行。

安装程序用户始终是系统管理员,因此具有所有特权。

As part of installer code, we are trying to make changes to IIS (check and create virtual directory, followed by adding a section to its web.config file.

Installer was working fine till recently a strange error started haunting us and blocking the installer from proceeding

Error says /Core/Service/UserService failed. Reason: Filename: \?\ c:\website1\Core\Common\Service\userService\web.config Error: cannot write configuration file

Please note that "Default Web Site/Core/Service/UserService" is a virtual directory under
"Default Web Site/Core" virtual directory in IIS.

"Default Web Site/Core" has physical path of c:\website1\Core\Common whereas "Default Web Site/Core/Service/UserService" is created with physical path of "c:\website1\Core\Service\UserService"

Not sure why error is pointing towards wrong folder path for web.config.

Code we have used is like this

using (ServerManager mgr = new ServerManager())
{
  //code to add virtual path to root website (default web site)
  mgr.CommitChanges();
}
using (ServerManager mgrpolicy = new ServerManager())
{
  Configuration config = mgrpolicy.GetWebConfiguration("Default Web Site","/Core/Service/UserService");
ConfigurationSection hndlesection = config.GetSection("system.webserver/handlers");
hndlesection["accessPolicy"] = "Read,Write"; //this value is dynamic based on installer input but hardcoded here for your reference.

  mgrpolicy.CommitChanges(); //issue comes after this line
}

we are running this installer on .net 4.8 windows application where the above code runs as part of a class library pointing to namespace System.Web.Administration.

Installer user is always system administrator and thus has all privileges.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文