1

我正在生成URI如下(此代码已简化和伪造):

Uri baseUri = "http://localhost/MyApp/Account/Login";
Uri fullUri = GetFullUri(baseUri, user);

GetFullUri 看起来像这样(这是在 .NET 2 程序集中):

public Uri GetFullUri(Uri baseUri, User user)
{
    string token = GetTokenFromUser(user); //Implementation not important.

    //Create a new URI based on the base URI, adding a query string.
    return new Uri(baseUri, string.Format("?Token={0}", token));
}

GetFullUri从 .NET 4 程序集调用,结果是正确的,fullUri如下所示:

http://localhost/MyApp/Account/Login?Token=ABC123

然后我从 .NET 2 程序集中调用了相同的确切代码,结果不正确,fullUri如下所示:

http://localhost/MyApp/Account/?Token=ABC123

请注意 .NET 2 结果是如何缺少第 4 段也是最后一段“登录”?这有什么关系?

4

1 回答 1

1

看起来像是 .NET 4.0 中修复的错误。尝试使用UriBuilder,它适用于以下两种情况:

public Uri GetFullUri(Uri baseUri, User user)
{
   string token = GetTokenFromUser(user); //Implementation not important.

   var builder = new UriBuilder(baseUri);
   builder.Query = string.Format("Token={0}", token);
   return builder.Uri;
}
于 2012-11-30T16:51:13.297 回答