相关疑难解决方法(0)

179
推荐指数
5
解决办法
8万
查看次数

来自C#中基数+相对URL的绝对URL

我有一个基本网址:

http://my.server.com/folder/directory/sample
Run Code Online (Sandbox Code Playgroud)

还有一个相对的:

../../other/path
Run Code Online (Sandbox Code Playgroud)

如何从中获取绝对URL?使用字符串操作非常简单,但我想以安全的方式使用Uri类或类似的东西.

它适用于标准的C#应用​​程序,而不是ASP.NET应用程序.

.net c# url path

29
推荐指数
1
解决办法
2万
查看次数

URL的Path.Combine(第2部分)

一段时间以来,我一直在寻找一种适用于URL的Path.Combine方法.这与URL的Path.Combine类似有一个很大的区别.

我将以一个例子来说明.假设我们有一个基本网址:http://example.com/somefolder和一个文件:foo.txt.因此,完整的路径将是:http://example.com/somefolder/foo.txt.听起来很简单吧?哈.

我尝试了Uri课:Uri.TryCreate(new Uri("http://example.com/somefolder"), "foo.txt", out x);结果"http://example.com/foo.txt".

然后我尝试了Path:System.IO.Path.Combine("http://example.com/somefolder", "foo.txt");结果"http://example.com/somefolder\foo.txt"......更近,但仍然没有.

为了踢,我接着尝试了:System.IO.Path.Combine("http://example.com/somefolder/", "foo.txt")结果"http://example.com/somefolder/foo.txt".

最后一个工作,但它基本上在那时进行字符串连接.

所以我认为我有两个选择:

  • 使用Path.Combine并将所有\替换为/
  • 使用基本字符串连接

我错过了一个内置的框架方法吗?

更新:我的用例是下载一堆文件.我的代码看起来像这样:

    public void Download()
    {
        var folder = "http://example.com/somefolder";
        var filenames = getFileNames(folder);

        foreach (var name in filenames)
        {
            downloadFile(new Uri(folder + "/" + name));
        }
    }
Run Code Online (Sandbox Code Playgroud)

我不得不在Uri构造函数中使用字符串concat,并且还要检查是否需要斜杠(我在代码中省略了).

在我看来,我想要做的事情会出现很多,因为Uri类除了http之外还处理很多其他协议.

c# uri path

19
推荐指数
2
解决办法
2万
查看次数

在.NET中是否存在与Path.PathSeparator等效的URI?

我需要从字符串值构建一个URI路径,然后将它们组合在一起.是否存在/分隔符值的URI常量Path.PathSeparator

.net uri

18
推荐指数
1
解决办法
6436
查看次数

在.NET中创建Uri会自动对来自传递字符串的所有参数进行编码

假设我想从以下字符串创建一个Uri对象:

string url = @"http://someserver.com?param1=1&url=http%3a%2f%2fwww.otherserver.com";
Uri uri = new Uri(url, UriKind.Absolute);
Run Code Online (Sandbox Code Playgroud)

预期结果将是:

http://someserver.com?param1=1&url=http%3a%2f%2fwww.otherserver.com 
Run Code Online (Sandbox Code Playgroud)

获得:

http://someserver.com/?param1=1&url=http://www.otherserver.com
Run Code Online (Sandbox Code Playgroud)

在允许创建Uri的许多相关方法中注意到相同的行为:Uri.TryCreate,UriBuilder.Uri等.

如何获得保留初始编码参数的Uri?

.net c# url escaping

8
推荐指数
3
解决办法
7108
查看次数

为什么UriBuilder.query没有转义(url编码)查询字符串?

UriBuilder.Query属性"包含URI中包含的任何查询信息." 根据文档,"查询信息根据RFC 2396进行转义."

基于此,并且由于此属性是可写的,我假设在设置它时,System.UriBuilder将解析您的查询字符串,并根据RFC 2396转义(url编码).特别是,{和}不在非保留字符集中,所以他们应该根据RFC 2396的第9页进行转义.但是,似乎System.UriBuilder没有做任何逃避.

我是否需要手动Server.URLEncode params,还是有办法System.UriBuilder处理编码?

这是我的示例代码.你可以在ideone.com上运行它,看看确实没有URL编码.

using System;

public class Test
{
    public static void Main()
    {
        var baseUrl = new System.Uri("http://www.bing.com");
        var builder = new System.UriBuilder(baseUrl);
        string name = "param";
        string val = "{'blah'}";
        builder.Query = name + "=" + val;

        // Try several different ouput methods; none will be URL encoded
        Console.WriteLine(builder.ToString());
        Console.WriteLine(builder.Uri.ToString());
        Console.WriteLine(builder.Query);
    }
}
Run Code Online (Sandbox Code Playgroud)

.net c#

8
推荐指数
1
解决办法
6276
查看次数

如何为代理控制器设置Web API路由?

我的部分应用程序需要充当第三方RESTful Web服务的代理服务器.有没有办法设置Web API路由,以便所有相同类型的请求将使用相同的方法?

例如,如果客户端发送这些GET请求中的任何一个,我希望它们进入单个GET操作方法,然后将该请求发送到下游服务器.

api/Proxy/Customers/10045
api/Proxy/Customers/10045/orders
api/Proxy/Customers?lastname=smith
Run Code Online (Sandbox Code Playgroud)

GET的单一操作方法将获取这三个请求中的任何一个并将它们发送到相应的服务(我知道如何使用HttpClient来有效地实现这一点):

http://otherwebservice.com/Customers/10045
http://otherwebservice.com/Customers/10045/orders
http://otherwebservice.com/Customers?lastname=smith
Run Code Online (Sandbox Code Playgroud)

我不想将我的Web服务紧密地耦合到第三方Web服务,并将其整个API作为我内部的方法调用进行复制.

我想到的一个解决方法是简单地在客户端上用JavaScript编码目标URL,并将其传递给Web API,然后只能看到一个参数.它会工作,但我更愿意在可能的情况下使用Web API中的路由功能.

proxy asp.net-web-api

7
推荐指数
1
解决办法
9655
查看次数

合并两个URI时,路径部分会被覆盖

我需要组合两个包含.Path信息的URL.

我想使用Uri.TryCreate()的可能性,所以我可以捕获格式错误的URL.

我面临的问题是,当我合并绝对URI和相对URI时,似乎忽略了基URI路径:

Uri absoluteUri= new Uri("http://hostname/path/", UriKind.Absolute);
Uri relativeUri = new Uri("/my subsite/my page.aspx?my=query", UriKind.Relative);
Uri resultUri;
if (!Uri.TryCreate(absoluteUri, relativeUri, out resultUri))
      // handle errors
Run Code Online (Sandbox Code Playgroud)

以上输出是:

http://hostname/my%20subsite/my%20page.aspx?my=query
Run Code Online (Sandbox Code Playgroud)

我希望它是:

http://hostname/path/my%20subsite/my%20page.aspx?my=query
Run Code Online (Sandbox Code Playgroud)

有没有办法组合使用Uri类包含路径信息的URL ?

c# api url uri

6
推荐指数
1
解决办法
2085
查看次数

Uri(Uri,String)构造函数不能正常工作?

这是我的代码的一部分:

Uri branches = new Uri(@"https://127.0.0.1:8443/svn/CXB1/Validation/branches");
Uri testBranch = new Uri(branches, "test");
Run Code Online (Sandbox Code Playgroud)

我希望testBranches会是https://127.0.0.1:8443/svn/CXB1/Validation/branches/test,但确实如此https://127.0.0.1:8443/svn/CXB1/Validation/test.我无法理解为什么Uri(Uri,string)构造函数吃掉路径的最后部分.

.net c#

6
推荐指数
1
解决办法
446
查看次数

在PowerShell中使用分隔符连接部件的最佳方法

我需要将多个Url元素连接成一个字符串,因此我编写了通用的Join-Parts函数:

filter Skip-Null { $_|?{ $_ } }

function Join-Parts
{
    param
    (
        $Parts = $null,
        $Separator = ''
    )

    [String]$s = ''
    $Parts | Skip-Null | ForEach-Object {
        $v = $_.ToString()
        if ($s -ne '')
        {
            if (-not ($s.EndsWith($Separator)))
            {
                if (-not ($v.StartsWith($Separator)))
                {
                    $s += $Separator
                }
                $s += $v
            }
            elseif ($v.StartsWith($Separator))
            {
                $s += $v.SubString($Separator.Length)
            }
        }
        else
        {
            $s = $v
        }
    }
    $s
}

Join-Parts -Separator '/' -Parts 'http://mysite','sub/subsub','/one/two/three'
Join-Parts -Separator '/' -Parts 'http://mysite',$null,'one/two/three' …
Run Code Online (Sandbox Code Playgroud)

powershell powershell-2.0

5
推荐指数
4
解决办法
9048
查看次数

如何将URL与查询字符串部分组合保留?

我正在编写一个需要调用webapp的C#应用​​程序.我试图用来System.Uri组合两个URL:一个基本URL和我需要的webapp的特定服务(或许多)的相对路径.我有一个名为webappBackendURL我想在一个地方定义的类成员,所有调用都是从它构建的(webappBackendURL未定义为我的示例所示的接近).

System.Uri webappBackendURL = new System.Uri("http://example.com/");
System.Uri rpcURL           = new System.Uri(webappBackendURL,"rpc/import");
// Result: http://example.com/rpc/import
Run Code Online (Sandbox Code Playgroud)

但是,如果webappBackendURL包含查询字符串,则无法保留.

System.Uri webappBackendURL = new System.Uri("http://example.com/?authtoken=0x0x0");
System.Uri rpcURL           = new System.Uri(webappBackendURL,"rpc/import");
// Result: http://example.com/rpc/import <-- (query string lost)
Run Code Online (Sandbox Code Playgroud)

是否有更好的方法组合URL?.NET库很广泛,所以我想我可能只是忽略了一种内置的方法来处理这个问题.理想情况下,我希望能够组合这样的URL:

System.Uri webappBackendURL = new System.Uri("http://example.com/?authtoken=0x0x0");
System.Uri rpcURL           = new System.Uri(webappBackendURL,"rpc/import?method=overwrite&runhooks=true");
// Result: http://example.com/rpc/import?authtoken=0x0x0&method=overwrite&runhooks=true
Run Code Online (Sandbox Code Playgroud)

c# url

5
推荐指数
1
解决办法
3501
查看次数

c#razor字符串与html字符串的串联

我有 baseUrl = "http://localhost:10232";

我在我的视图中使用它,如下所示:

<a href='@mynamespace.Controllers.MyVars.baseUrl/Tickets/Create'>Create New</a>
Run Code Online (Sandbox Code Playgroud)

它给了我很好的输出,即

<a href='http://localhost:10232/Tickets/Create'>Create New</a>
Run Code Online (Sandbox Code Playgroud)

但如果我想/成为http://localhost:10232/(现在我在最后加了一个斜线)

那么有没有办法像上面那样产生相同的结果?我尝试了以下方式

<a href='@mynamespace.Controllers.MyVars.baseUrl+Tickets/Create'>Create New</a>
Run Code Online (Sandbox Code Playgroud)

但是连接在html中不起作用,所以我怎样才能实现它(用html字符串连接ac变量)

c# asp.net-mvc razor

5
推荐指数
1
解决办法
2万
查看次数

ASP.NET:URI处理

我正在写一个方法,让我们说,给予1hello应该返回http://something.com/?something=1&hello=en.

可以很容易地将它们组合在一起,但ASP.NET 3.5为构建URI提供了哪些抽象功能?我喜欢这样的东西:

URI uri = new URI("~/Hello.aspx"); // E.g. ResolveUrl is used here
uri.QueryString.Set("something", "1");
uri.QueryString.Set("hello", "en");
return uri.ToString(); // /Hello.aspx?something=1&hello=en
Run Code Online (Sandbox Code Playgroud)

我发现这个Uri课程听起来非常相关,但我找不到任何能真正完成上述课程的课程.有任何想法吗?

(对于它的价值,参数的顺序对我来说无关紧要.)

asp.net uri query-string

4
推荐指数
1
解决办法
9161
查看次数