如何通过代码启用对 URI-Class 的 IDN/IRI 支持? [英] How to enable IDN/IRI Support for URI-Class by code?

查看:17
本文介绍了如何通过代码启用对 URI-Class 的 IDN/IRI 支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 URI 类启用 IDN/IRI 支持,因为我需要德语变音域(例如 www.bücher.de)上的Uri.IsWellFormedUriString"方法.

我在 https://stackoverflow.com/a/6107682/413531(摘自 http://msdn.microsoft.com/en-us/library/system.uri.aspx 在国际资源标识符支持"),但该解决方案对我不起作用.我当前的 app.config 文件看起来像这样:

<预><代码><配置><configSections><sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" ><section name="..." type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/><!-- ...这里的某些部分...--></sectionGroup><sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" ><!-- ...这里的一些部分...--></sectionGroup></configSections><用户设置><!-- ...这里的一些设置...--></userSettings><应用程序设置><!-- ...这里的一些设置...--></applicationSettings></配置>

当我刚刚添加

 <idn enabled="全部"/><iriParsing enabled="true"/></uri>

作为最后的另一个孩子,抛出异常:ConfigurationErrorsException - {"Das Konfigurationssystem konnte nicht initialisiert werden."}

所以我在 http://msdn.microsoft 中进一步阅读.com/en-us/library/system.uri.aspx 遇到了

<块引用>

Uri 类中的 IRI 和 IDN 处理也可以使用System.Configuration.IriParsingElement,System.Configuration.IdnElement 和 System.Configuration.UriSection配置设置类.这System.Configuration.IriParsingElement 设置启用或禁用 IRI在 Uri 类中处理.System.Configuration.IdnElement设置启用或禁用 Uri 类中的 IDN 处理.这System.Configuration.IriParsingElement 设置也间接控制 IDN.必须启用 IRI 处理才能进行 IDN 处理可能的.如果禁用 IRI 处理,则 IDN 处理将被禁用设置为 .NET Framework 2.0 行为所在的默认设置用于兼容性,不使用 IDN 名称.

不幸的是,我找不到 System.Configuration.IriParsingElement、System.Configuration.IdnElement 和 System.Configuration.UriSection 的使用示例.我不知道这些是如何使用的...

所以基本上,我的问题归结为:我想在 URI 类中启用 IDN/IRI 支持,但我不知道如何做到这一点.config-solution 对我不起作用,所以我想通过代码尝试,但不知道如何.顺便提一句.我也想知道为什么配置的东西不起作用;)

解决方案

我找到了一个解决方案 - 至少对于 app.config 部分.根据 http://social.msdn.microsoft.com/Forums/nl-NL/netfxnetcom/thread/56a73dbb-c0d4-4cad-876d-83ad74064393 如果.Net 版本低于 4.0,则必须在 app.config 中添加一行使用:

<section name="uri" type="System.Configuration.UriSection, System,版本=2.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089"/></configSections>

添加此行后,我还可以添加

<idn enabled="全部"/><iriParsing enabled="true"/></uri>

没有得到错误.我的完整 app.config 现在看起来像这样:

<预><代码><配置><configSections><sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" ><section name="..." type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/><!-- ...这里的某些部分...--></sectionGroup><sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" ><!-- ...这里的某些部分...--></sectionGroup><!-- ...插入缺失的部分...--><section name="uri" type="System.Configuration.UriSection, System,版本=2.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089"/></configSections><!-- ... 插入 URI 设置 ... --><uri><idn enabled="全部"/><iriParsing enabled="true"/></uri><用户设置><!-- ...这里的一些设置...--></userSettings><应用程序设置><!-- ...这里的一些设置...--></applicationSettings></配置>

I'm trying to enable the IDN/IRI support for the URI class, because I need the "Uri.IsWellFormedUriString" method on german umlaut-domains (e.g. www.bücher.de).

I found similar question at https://stackoverflow.com/a/6107682/413531 (taken from http://msdn.microsoft.com/en-us/library/system.uri.aspx at "International Resource Identifier Support") but the solution is not working for me. My current app.config file looks something like this:

<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="..." type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        <!-- ... some sections in here ... -->
        </sectionGroup>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <!-- ... some sections in here ... -->
        </sectionGroup>
    </configSections>
    <userSettings>
        <!-- ... some settings in here ... -->
    </userSettings>
    <applicationSettings>
        <!-- ... some settings in here ... -->
    </applicationSettings>
</configuration>

when I just add

  <uri>
  <idn enabled="All" />
  <iriParsing enabled="true" />
  </uri>

as an other child of at the end, an exception is thrown: ConfigurationErrorsException - {"Das Konfigurationssystem konnte nicht initialisiert werden."}

So I read further in http://msdn.microsoft.com/en-us/library/system.uri.aspx an came across

IRI and IDN processing in the Uri class can also be controlled using the System.Configuration.IriParsingElement, System.Configuration.IdnElement, and System.Configuration.UriSection configuration setting classes. The System.Configuration.IriParsingElement setting enables or disables IRI processing in the Uri class. The System.Configuration.IdnElement setting enables or disables IDN processing in the Uri class. The System.Configuration.IriParsingElement setting also indirectly controls IDN. IRI processing must be enabled for IDN processing to be possible. If IRI processing is disabled, then IDN processing will be set to the default setting where the .NET Framework 2.0 behavior is used for compatibility and IDN names are not used.

Unfortunatly I was not able to find an example for the usage of System.Configuration.IriParsingElement, System.Configuration.IdnElement, and System.Configuration.UriSection. I've no clue how these are used...

So basically, my problem comes down to: I want to enable IDN/IRI support in the URI class, but I can't figure out how to do this. The config-solution is not working for me, so I would like to try it by code, but can't figure out how. Btw. I would also like to know why the config thing isn't working ;)

解决方案

I found a solution - at least for the app.config part. According to http://social.msdn.microsoft.com/Forums/nl-NL/netfxnetcom/thread/56a73dbb-c0d4-4cad-876d-83ad74064393 one has to add an additional line in the app.config if a .Net version lower 4.0 is used:

<configSections>
  <section name="uri" type="System.Configuration.UriSection, System,
                      Version=2.0.0.0, Culture=neutral,
                      PublicKeyToken=b77a5c561934e089" />
</configSections>

After adding this line, I could also add

<uri>
    <idn enabled="All" />
    <iriParsing enabled="true" />
</uri>

without getting an error. My complete app.config now looks like this:

<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="..." type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    <!-- ... some sections in here ... -->
        </sectionGroup>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
    <!-- ... some sections in here ... -->
        </sectionGroup>

<!-- ... insert Missing section ... -->

        <section name="uri" type="System.Configuration.UriSection, System,
                      Version=2.0.0.0, Culture=neutral,
                      PublicKeyToken=b77a5c561934e089" />

    </configSections>

<!-- ... insert URI settings ... -->

    <uri>
        <idn enabled="All" />
        <iriParsing enabled="true" />
    </uri>

    <userSettings>
    <!-- ... some settings in here ... -->
    </userSettings>
    <applicationSettings>
    <!-- ... some settings in here ... -->
    </applicationSettings>
</configuration>

这篇关于如何通过代码启用对 URI-Class 的 IDN/IRI 支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆