ฉันมี 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
จากมัน
คุณสามารถบอกฉันว่าฉันจะได้รับมันได้อย่างไร
คำตอบ:
คุณสามารถใช้ได้ System.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 ในความคิดเห็น
substring
วิธีการที่จะทำให้เกิดข้อผิดพลาดถ้าไม่มีสตริงแบบสอบถาม ใช้string path = url.Substring(0, url.IndexOf("?") > 0? url.IndexOf("?") : url.Length);
แทน
นี่เป็นวิธีที่ง่ายกว่า:
var uri = new Uri("http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye");
string path = uri.GetLeftPart(UriPartial.Path);
ยืมมาจากที่นี่: การตัดสตริงข้อความค้นหา & ส่งคืน URL ที่สะอาด C # ASP.net
return Request.Url.GetLeftPart(UriPartial.Path);
uri.GetComponent(
เป็นอีกวิธีที่ยอดเยี่ยมในการรับชิ้นส่วนของ Uri ฉันไม่รู้เกี่ยวกับสองสิ่งนี้จนกระทั่งตอนนี้!
นี่คือทางออกของฉัน:
Request.Url.AbsoluteUri.Replace(Request.Url.Query, String.Empty);
Request.RawUrl.Split(new[] {'?'})[0];
RawUrl
จะได้รับของ URL ก่อนที่จะปรับเปลี่ยน URL ดังนั้นบางทีใช้AbsoluteUri
? Request.Url.AbsoluteUri.Split('?')[0]
คำตอบที่ดียังพบที่นี่แหล่งที่มาของคำตอบ
Request.Url.GetLeftPart(UriPartial.Path)
ทางของฉัน:
new UriBuilder(url) { Query = string.Empty }.ToString()
หรือ
new UriBuilder(url) { Query = string.Empty }.Uri
Uri.GetLeftPart
โครงการเพราะมันไม่ได้มีวิธีการ NET Core รุ่นล่าสุด (1.1) ควรมีวิธีการนั้น (ไม่สามารถยืนยันได้เพราะตอนนี้ฉันไม่ได้อยู่ในเน็ตคอร์ 1.1)
คุณสามารถใช้Request.Url.AbsolutePath
เพื่อรับชื่อหน้าและRequest.Url.Authority
สำหรับชื่อโฮสต์และพอร์ต ฉันไม่เชื่อว่ามีทรัพย์สินในตัวเพื่อให้สิ่งที่คุณต้องการ แต่คุณสามารถรวมเข้าด้วยกันได้
รูปแบบแยก ()
ฉันแค่ต้องการเพิ่มรูปแบบนี้สำหรับการอ้างอิง URL ที่มักจะสตริงและดังนั้นจึงเป็นเรื่องง่ายที่จะใช้วิธีการมากกว่าSplit()
Uri.GetLeftPart()
และSplit()
ยังสามารถทำงานกับค่าสัมพัทธ์ค่าว่างและค่าว่างได้ในขณะที่ Uri ส่งข้อยกเว้น นอกจากนี้ URL อาจมีแฮชเช่น/report.pdf#page=10
(ซึ่งเปิดไฟล์ PDF ที่หน้าเว็บเฉพาะ)
วิธีการต่อไปนี้เกี่ยวข้องกับ URL ประเภทเหล่านี้ทั้งหมด:
var path = (url ?? "").Split('?', '#')[0];
ตัวอย่างผลลัพธ์:
http: //domain/page.html#page=2 ---> http: //domain/page.html
page.html ---> page.html
นี่คือวิธีการขยายโดยใช้คำตอบของ @ Kolman จำง่ายกว่าเล็กน้อยที่จะใช้ Path () กว่า GetLeftPart คุณอาจต้องการเปลี่ยนเส้นทางเป็น 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);
}
}
}
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
Request.RawUrl.Split('?')[0]
สำหรับชื่อ URL เท่านั้น !!
โซลูชันสำหรับ 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("?"));
var canonicallink = Request.Url.Scheme + "://" + Request.Url.Authority + Request.Url.AbsolutePath.ToString();
ลองสิ่งนี้:
urlString=Request.RawUrl.ToString.Substring(0, Request.RawUrl.ToString.IndexOf("?"))
จากนี้: http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye คุณจะได้รับสิ่งนี้: mypage.aspx
this.Request.RawUrl.Substring(0, this.Request.RawUrl.IndexOf('?'))