C#中的URL拆分?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1029909/
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-08-06 06:32:44  来源:igfitidea点击:

URL split in C#?

c#asp.net

提问by

I have a url like example.com/page?a=1&ret=/user/page2.

我有一个像example.com/page?a=1&ret=/user/page2.

I was using string.split('/') to figure out the paths but this case you can see it isn't very useful. How do i split the URL so i can get the page path?

我正在使用 string.split('/') 来找出路径,但在这种情况下,您可以看到它不是很有用。如何拆分 URL 以便获取页面路径?

采纳答案by dahlbyk

If you make a System.Uri object from your string, it will have several properties for different parts of the path:

如果您从字符串创建 System.Uri 对象,它将为路径的不同部分提供多个属性:

string path = "http://example.com/page?a=1&ret=/user/page2";
Uri uri = new Uri(path);
Console.WriteLine(uri.AbsolutePath); // Prints "/page"

回答by Robin Day

The Request.Url (Uri) object has a lot of useful properties relating to the path. It can give you the entire QueryString to take off of the full url if that's what you're after?

Request.Url (Uri) 对象具有许多与路径相关的有用属性。如果这就是您所追求的,它可以为您提供整个 QueryString 以摆脱完整的 url?

You can also perform a Server.MapPath on the page itself and then use the FileInfo object to view various parts of the file itself.

您还可以在页面本身上执行 Server.MapPath,然后使用 FileInfo 对象查看文件本身的各个部分。

回答by heavyd

You could load it into an URIobject and get the Uri.AbsolutePathproperty.

您可以将其加载到URI对象中并获取Uri.AbsolutePath属性。

回答by dahlbyk

Check out System.Uri class. It'll parse out your url into fragments.

查看 System.Uri 类。它会将您的网址解析为片段。

回答by bytebender

Have you considered using the UriBuilder... see stack over question 479799

您是否考虑过使用 UriBuilder... 请参阅问题 479799 上的堆栈

Use that first then split up the .Path property

首先使用它然后拆分 .Path 属性

回答by bytebender

This seems like a good case to use System.Uri:

这似乎是一个很好的使用案例System.Uri

Uri uri = new Uri("example.com/page?a=1&ret=/user/page2");
System.Windows.Forms.MessageBox.Show(
"Absolute URI: " + uri.AbsoluteUri + "\r\n" +
"Absolute Path: " + uri.AbsolutePath + "\r\n" +
"Local path: " + uri.LocalPath + "\r\n" +
"Host: " + uri.Host + "\r\n" +
"Port: " + uri.Port + "\r\n" +
"Query: " + uri.Query + "\r\n");

回答by Richard Anthony Hein

Assuming you mean you want to get the "page2" bit:

假设你的意思是你想获得“page2”位:

 var ub = new UriBuilder("example.com/page?a=1&ret=/user/page2");
 NameValueCollection nvc = HttpUtility.ParseQueryString(ub.Query);
 string page = nvc[nvc.Count - 1]; // gets "/user/page2"

Then you'll have to use split on the rest.

然后你将不得不在其余部分使用 split 。

Edit: Well, you could use System.IO.Path.GetFileNameWithoutExtension(page) to return "page2", but I am not sure it feels right to me.

编辑:嗯,您可以使用 System.IO.Path.GetFileNameWithoutExtension(page) 返回“page2”,但我不确定这对我来说是否正确。

System.IO.Path.GetFileNameWithoutExtension("example.com/page?a=1&ret=/user/page2")returns "page2" as well.

System.IO.Path.GetFileNameWithoutExtension("example.com/page?a=1&ret=/user/page2")也返回“page2”。

回答by Rony

you might also consider using the Routing API buit inside ASP.net 2.0 which will give you fine grained control over your URL routes

您也可以考虑在 ASP.net 2.0 中使用 Routing API buit,它可以让您对 URL 路由进行细粒度控制

回答by Jeff Meatball Yang

Is this an ASP.NET project? In your HttpHandler/Page you can simply use the Request object:

这是一个 ASP.NET 项目吗?在您的 HttpHandler/Page 中,您可以简单地使用 Request 对象:

string path = HttpContext.Request.Path;

If you don't have an HttpContext, System.Uri gives you something similar:

如果你没有 HttpContext,System.Uri 会给你类似的东西:

string path = new Uri("example.com/page?a=1&ret=/user/page2").AbsolutePath;