我有一个这样的URL:
http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye
我想从中得到http://www.example.com/mypage.aspx。
你能告诉我怎么买吗?
我有一个这样的URL:
http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye
我想从中得到http://www.example.com/mypage.aspx。
你能告诉我怎么买吗?
您可以使用Request.Url.AbsolutePath获取页面名,使用Request.Url.Authority获取主机名和端口。我不相信有一个内置的属性可以给你想要的东西,但你可以自己组合它们。
你可以使用系统。Uri
Uri url = new Uri("http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye");
string path = String.Format("{0}{1}{2}{3}", url.Scheme,
Uri.SchemeDelimiter, url.Authority, url.AbsolutePath);
或者你可以用substring
string url = "http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye";
string path = url.Substring(0, url.IndexOf("?"));
编辑:修改第一个解决方案,以反映brillyfresh在评论中的建议。
这里有一个更简单的解决方案:
var uri = new Uri("http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye");
string path = uri.GetLeftPart(UriPartial.Path);
借用这里:截断查询字符串和返回干净的URL c# ASP.net
我的方法:
new UriBuilder(url) { Query = string.Empty }.ToString()
or
new UriBuilder(url) { Query = string.Empty }.Uri
下面是一个使用@Kolman答案的扩展方法。记住使用Path()比记住GetLeftPart要容易一些。你可能想要重命名Path为GetPath,至少在他们将扩展属性添加到c#之前。
用法:
Uri uri = new Uri("http://www.somewhere.com?param1=foo¶m2=bar");
string path = uri.Path();
类:
using System;
namespace YourProject.Extensions
{
public static class UriExtensions
{
public static string Path(this Uri uri)
{
if (uri == null)
{
throw new ArgumentNullException("uri");
}
return uri.GetLeftPart(UriPartial.Path);
}
}
}
试试这个:
urlString=Request.RawUrl.ToString.Substring(0, Request.RawUrl.ToString.IndexOf("?"))
从这里:http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye 你会得到这个:mypage.aspx
Silverlight的解决方案:
string path = HtmlPage.Document.DocumentUri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);
我已经创建了一个简单的扩展,因为其他一些答案抛出空异常,如果没有QueryString开始:
public static string TrimQueryString(this string source)
{
if (string.IsNullOrEmpty(source))
return source;
var hasQueryString = source.IndexOf('?') != -1;
if (!hasQueryString)
return source;
var result = source.Substring(0, source.IndexOf('?'));
return result;
}
用法:
var url = Request.Url?.AbsoluteUri.TrimQueryString()
简单的例子是使用子字符串:
string your_url = "http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye";
string path_you_want = your_url .Substring(0, your_url .IndexOf("?"));
Split()变化
我只是想添加这个变体供参考。url通常是字符串,因此使用Split()方法比Uri.GetLeftPart()更简单。当Uri抛出异常时,Split()也可以处理相对值、空值和空值。此外,url还可能包含散列,例如/report.pdf#page=10(在特定页面打开pdf)。
下面的方法处理所有这些类型的url:
var path = (url ?? "").Split('?', '#')[0];
示例输出:
Null——>为空 空——>空 http://domain/page.html——> http://domain/page.html http://domain/page.html?q=100——> http://domain/page.html http://domain/page.html?q=100#page=2——> http://domain/page.html http://domain/page.html#page=2——> http://domain/page.html Page.html——> Page.html page.html吗?Q =100——> page.html page.html吗?Q =100#page=2——> page.html Page.html #散列——> Page.html
var canonicallink = Request.Url.Scheme + "://" + Request.Url.Authority + Request.Url.AbsolutePath.ToString();
string url = "http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye";
string path = url.split('?')[0];
System.Uri。GetComponents,只是指定你想要的组件。
Uri uri = new Uri("http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye");
uri.GetComponents(UriComponents.SchemeAndServer | UriComponents.Path, UriFormat.UriEscaped);
输出:
http://www.example.com/mypage.aspx