如何使的System.Uri不反向转义%2F(斜杠)的路径? [英] How to make System.Uri not to unescape %2f (slash) in path?

查看:301
本文介绍了如何使的System.Uri不反向转义%2F(斜杠)的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的System.Uri构造坚持进行反向转义%2F序列向前的斜线,当它看到他们在URL的路径部分。我怎么能避免behaiviour?

System.Uri constructor insists on unescaping %2f sequences as foward slashes, when it sees them in path portion of URL. How could I avoid that behaiviour?

我见过的<一个href="http://connect.microsoft.com/VisualStudio/feedback/details/94109/system-uri-constructor-evaluates-escaped-slashes-and-removes-double-slashes"相对=nofollow>连接票这个thig的,但解决办法是行不通的.NET 4,据就是我的理解。

I've seen Connect ticket on this thig, but workaround is not working for .net 4 as far is I understand.

推荐答案

它在内部保持了原始的字符串,例如,下面的code:

It keeps the original string internally, for example, the following code:

    Uri u = new Uri("http://www.example.com/path?var=value%2fvalue");
    Console.WriteLine(u.OriginalString);

显示

http://www.example.com/path?var=value%2fvalue

修改:我有更新的<一个发现了code href="http://connect.microsoft.com/VisualStudio/feedback/details/94109/system-uri-constructor-evaluates-escaped-slashes-and-removes-double-slashes"相对=nofollow>连接链路解决方法获得最新的.NET版本。在这里,它是:

EDIT: I have update the code found in the Connect Link Workaround for recent .NET versions. Here it is:

// System.UriSyntaxFlags is internal, so let's duplicate the flag privately
private const int UnEscapeDotsAndSlashes = 0x2000000;
private const int SimpleUserSyntax = 0x20000;

public static void LeaveDotsAndSlashesEscaped(Uri uri)
{
    if (uri == null)
        throw new ArgumentNullException("uri");

    FieldInfo fieldInfo = uri.GetType().GetField("m_Syntax", BindingFlags.Instance | BindingFlags.NonPublic);
    if (fieldInfo == null)
        throw new MissingFieldException("'m_Syntax' field not found");

    object uriParser = fieldInfo.GetValue(uri);
    fieldInfo = typeof(UriParser).GetField("m_Flags", BindingFlags.Instance | BindingFlags.NonPublic);
    if (fieldInfo == null)
        throw new MissingFieldException("'m_Flags' field not found");

    object uriSyntaxFlags = fieldInfo.GetValue(uriParser);

    // Clear the flag that we don't want
    uriSyntaxFlags = (int)uriSyntaxFlags & ~UnEscapeDotsAndSlashes;
    uriSyntaxFlags = (int)uriSyntaxFlags & ~SimpleUserSyntax;
    fieldInfo.SetValue(uriParser, uriSyntaxFlags);
}

当然,这是一个黑客,所以你应该在你自己的风险使用它: - )

Of course, it's a hack, so you should use it at your own risks :-)

这篇关于如何使的System.Uri不反向转义%2F(斜杠)的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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