![](/img/trans.png)
[英]Can't find my connection string tag in web.config file in ASP.NET MVC
[英]Why can't I open a custom web.config file in my web ASP app?
我無法在我的 ASP Web 項目中打開 web.config 文件。
我可以在我的項目中添加配置文件來設置授權頁面。
我需要打開這些頁面才能從我的管理頁面更新“角色”名稱。
按照微軟提供的示例后,它應該可以正常工作:
http://msdn.microsoft.com/en-us/library/vstudio/4c2kcht0(v=vs.100).aspx
這是我使用的 C# 代碼:
//Open the web.config custom
Configuration config = WebConfigurationManager.OpenWebConfiguration("~/WebAuthorizationPilotageGeneral") as Configuration;
//Get the authorization section
// section is always == NULL !!!!
-> section = (System.Web.Configuration.AuthorizationSection)config.GetSection("authorization");
//Remove all Roles before add news Roles
section.Rules.Clear();
autho = new System.Web.Configuration.AuthorizationRule(System.Web.Configuration.AuthorizationRuleAction.Allow);
foreach (string role in Roles.GetAllRoles())
{
if (role != ttbx_RoleName.Text)
{
autho.Roles.Add(role);
}
}
//Add the news Roles
section.Rules.Add(autho);
//Save the web.config custom
config.Save(ConfigurationSaveMode.Full, true);
我嘗試加載我的 web.config 自定義文件以更新“授權”部分
<!-- WebAuthorizationPilotageGeneral.config - access pilotage page -->
<authorization>
<allow roles="Administrator" />
<allow roles="Audit" />
<allow roles="Copil" />
<allow roles="System" />
<allow roles="User" />
<allow roles="visitor" />
<deny users="*" />
</authorization>
奇怪的是在加載配置文件后,我的配置對象仍然有 22 個部分,並且應該只有一個 => "authorization" ,它看起來像我的 (web.config) 根文件而不是 (WebAuthorizationPilotageGeneral) 配置文件。
更新:我嘗試將我的 4 個配置文件放在一個文件夾中
Web.config 文件
在 MS 示例中,使用了沒有“~”符號的相對配置路徑。 你有沒有嘗試刪除它?
// Set the root path of the Web application that contains the
// Web.config file that you want to access.
string configPath = "/MyAppRoot";
// Get the configuration object to access the related Web.config file.
Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.