visual-studio web.config、configSource 和“未声明‘xxx’元素”警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2833243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 11:58:15  来源:igfitidea点击:

web.config, configSource, and "The 'xxx' element is not declared" warning

asp.netxmlvisual-studioweb-config

提问by UpTheCreek

I have broken down the horribly unwieldy web.config file into individual files for some of the sections (e.g. connectionStrings, authentication, pages etc.) using the configSource attribute.

我已经使用 configSource 属性将极其笨拙的 web.config 文件分解为某些部分(例如 connectionStrings、身份验证、页面等)的单个文件。

This is working fine, but the individual xml files that hold the section 'snippets' cause warnings in VS.

这工作正常,但是保存“代码段”部分的各个 xml 文件会在 VS 中引起警告。

For example, a file named roleManager.config is used for the role manager section, and looks like this:

例如,一个名为 roleManager.config 的文件用于角色管理器部分,如下所示:

<roleManager enabled="false">
</rolemanager>

However I get a blue squiggle under the roleManager element in VS, and the following warning: The 'roleManager' element is not declared

但是,我在 VS 中的 roleManager 元素下看到一个蓝色波浪线,以及以下警告: The 'roleManager' element is not declared

I guess this is something to do with valid xml and schemas etc. Is there an easy way to fix this? Something I can add to the individual files?

我想这与有效的 xml 和模式等有关。有没有简单的方法来解决这个问题?我可以添加到单个文件中的内容吗?

Thanks

谢谢

P.S. I have heard that it is bad practice to break the web.config file out like this. But don't really understand why - can anyone illuminate me?

PS 我听说这样打破 web.config 文件是不好的做法。但真的不明白为什么 - 谁能照亮我?

回答by acromm

Searching a workaround to this matter using Custom Config Files, I found this solution. Dont know if is the correct one.

使用自定义配置文件搜索此问题的解决方法,我找到了此解决方案。不知道是否正确。

The problem is that VS cant find a schema to validate your .config (xml). If you are using "native" configuration elements or when you create your custom .config files you must set to every xml document a schema.

问题是 VS 找不到模式来验证您的 .config (xml)。如果您使用“本机”配置元素或在创建自定义 .config 文件时,您必须为每个 xml 文档设置一个架构。

By default (in VS9 for example) all xml files use \Microsoft Visual Studio 9.0\Xml\Schemas\DotNetConfig.xsd but you can add more schemas to use.

默认情况下(例如在 VS9 中)所有 xml 文件都使用 \Microsoft Visual Studio 9.0\Xml\Schemas\DotNetConfig.xsd,但您可以添加更多要使用的架构。

Before assigning a schema you must create it.

在分配架构之前,您必须创建它。

To create a new Schema based on your own custom.config:

要基于您自己的 custom.config 创建新架构:

  1. open your custom config file
  2. in menubar XML->Create Schema
  3. save it
  1. 打开您的自定义配置文件
  2. 在菜单栏 XML-> 创建架构
  3. 保存

To assign your schema:

要分配您的架构:

  1. open your custom config file
  2. in properties panel: click on the browse button [..]
  3. set the 'Use' column to your recently created schema
  1. 打开您的自定义配置文件
  2. 在属性面板中:单击浏览按钮 [..]
  3. 将“使用”列设置为您最近创建的架构

you can assign as many you want. or have one schema for all your different custom .config files

你可以分配任意数量的。或者为所有不同的自定义 .config 文件使用一个架构

(Sorry, but my English is not so good)

(对不起,我的英文不太好)

回答by Lanceomagnifico

I think that you get the blue squiggles since the schema of your web.config file doesn't declare these custom config sections that you've 'broken out' into individual files.

我认为你得到了蓝色波浪线,因为你的 web.config 文件的架构没有声明你已经“分解”成单个文件的这些自定义配置部分。

In investigating this, I see that some of my solutions have the same issue, but the config sections that are provided from microsoft DON'T have the squiggles. eg: we have extracted the appsettings and connectionstrings out into their own files, and they don't get the squiggles, but our custom ones do.

在对此进行调查时,我发现我的一些解决方案存在相同的问题,但 microsoft 提供的配置部分没有波浪线。例如:我们已经将 appsettings 和 connectionstrings 提取到了他们自己的文件中,他们没有得到波浪线,但我们的自定义有。

I tried to view the microsoft schema at schemas.microsoft.com/.netconfiguration/v2.0, but I get a 404 when trying to download it.

我试图在 schemas.microsoft.com/.netconfiguration/v2.0 上查看 microsoft 架构,但在尝试下载时收到 404。

What I'm trying to say is if you get a copy of the MS schema and alter it to include your external config files, you should be able to get rid of the dreaded squiggles!

我想说的是,如果您获得 MS 架构的副本并将其更改为包含您的外部配置文件,您应该能够摆脱可怕的波浪线!

HTH, Lance

HTH, 兰斯