我有一个这样的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。
你能告诉我怎么买吗?
当前回答
var canonicallink = Request.Url.Scheme + "://" + Request.Url.Authority + Request.Url.AbsolutePath.ToString();
其他回答
Request.RawUrl.Split(new[] {'?'})[0];
这是我的解决方案:
Request.Url.AbsoluteUri.Replace(Request.Url.Query, String.Empty);
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
试试这个:
urlString=Request.RawUrl.ToString.Substring(0, Request.RawUrl.ToString.IndexOf("?"))
从这里:http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye 你会得到这个:mypage.aspx
下面是一个使用@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);
}
}
}