รับ URL โดยไม่ต้องสอบถาม


189

ฉันมี URL เช่นนี้:

http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye

ฉันต้องการที่จะได้รับhttp://www.example.com/mypage.aspxจากมัน

คุณสามารถบอกฉันว่าฉันจะได้รับมันได้อย่างไร

คำตอบ:


129

คุณสามารถใช้ได้ 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 ในความคิดเห็น


6
url.AbsolutePath จะส่งกลับเฉพาะส่วนของ URL (/mypage.aspx) เท่านั้น prepend url.Scheme (http) + Uri.SchemeDelimiter (: //) + url.Authority (www.somesite.com) สำหรับ URL เต็มรูปแบบที่คุณต้องการ
Ryan

20
วิธี Uri.GetLeftPart นั้นง่ายกว่าดังที่stackoverflow.com/questions/1188096// ที่
Edward Wilde

substringวิธีการที่จะทำให้เกิดข้อผิดพลาดถ้าไม่มีสตริงแบบสอบถาม ใช้string path = url.Substring(0, url.IndexOf("?") > 0? url.IndexOf("?") : url.Length);แทน
stomy

378

นี่เป็นวิธีที่ง่ายกว่า:

var uri = new Uri("http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye");
string path = uri.GetLeftPart(UriPartial.Path);

ยืมมาจากที่นี่: การตัดสตริงข้อความค้นหา & ส่งคืน URL ที่สะอาด C # ASP.net


17
รุ่นหนึ่งบรรทัด:return Request.Url.GetLeftPart(UriPartial.Path);
jp2code

uri.GetComponent(เป็นอีกวิธีที่ยอดเยี่ยมในการรับชิ้นส่วนของ Uri ฉันไม่รู้เกี่ยวกับสองสิ่งนี้จนกระทั่งตอนนี้!
AaronLS


35
Request.RawUrl.Split(new[] {'?'})[0];

2
ฉันชอบสิ่งนี้เพียงเพื่อความจริงที่ว่าคุณสามารถใช้มันได้โดยไม่ต้องมียูริเต็ม
KingOfHypocrites

เก็บไว้ในใจว่าRawUrlจะได้รับของ URL ก่อนที่จะปรับเปลี่ยน URL ดังนั้นบางทีใช้AbsoluteUri? Request.Url.AbsoluteUri.Split('?')[0]
ป้องกันหนึ่ง


16

ทางของฉัน:

new UriBuilder(url) { Query = string.Empty }.ToString()

หรือ

new UriBuilder(url) { Query = string.Empty }.Uri

นี่คือสิ่งที่ฉันจะใช้สำหรับ NET แกน 1.0 Uri.GetLeftPartโครงการเพราะมันไม่ได้มีวิธีการ NET Core รุ่นล่าสุด (1.1) ควรมีวิธีการนั้น (ไม่สามารถยืนยันได้เพราะตอนนี้ฉันไม่ได้อยู่ในเน็ตคอร์ 1.1)
psulek

1
ฉันชอบอันนี้เพราะการสร้าง URIs เป็นสิ่งที่ UriBuilder ใช้ คำตอบอื่น ๆ ทั้งหมดคือแฮ็ก (ดี)
Nine Tails

11

คุณสามารถใช้Request.Url.AbsolutePathเพื่อรับชื่อหน้าและRequest.Url.Authorityสำหรับชื่อโฮสต์และพอร์ต ฉันไม่เชื่อว่ามีทรัพย์สินในตัวเพื่อให้สิ่งที่คุณต้องการ แต่คุณสามารถรวมเข้าด้วยกันได้


1
มันให้ฉัน / mypage.aspx ไม่ใช่สิ่งที่ฉันต้องการ
Rocky Singh

4

รูปแบบแยก ()

ฉันแค่ต้องการเพิ่มรูปแบบนี้สำหรับการอ้างอิง URL ที่มักจะสตริงและดังนั้นจึงเป็นเรื่องง่ายที่จะใช้วิธีการมากกว่าSplit() Uri.GetLeftPart()และSplit()ยังสามารถทำงานกับค่าสัมพัทธ์ค่าว่างและค่าว่างได้ในขณะที่ Uri ส่งข้อยกเว้น นอกจากนี้ URL อาจมีแฮชเช่น/report.pdf#page=10 (ซึ่งเปิดไฟล์ PDF ที่หน้าเว็บเฉพาะ)

วิธีการต่อไปนี้เกี่ยวข้องกับ URL ประเภทเหล่านี้ทั้งหมด:

   var path = (url ?? "").Split('?', '#')[0];

ตัวอย่างผลลัพธ์:


1
ฉันค่อนข้างตกใจที่ไม่ได้มีผู้ลงคะแนนใด ๆ จนกระทั่งฉันเอง นี่เป็นทางออกที่ดี
สัน

3

นี่คือวิธีการขยายโดยใช้คำตอบของ @ Kolman จำง่ายกว่าเล็กน้อยที่จะใช้ Path () กว่า GetLeftPart คุณอาจต้องการเปลี่ยนเส้นทางเป็น GetPath อย่างน้อยก็จนกว่าพวกเขาจะเพิ่มคุณสมบัติส่วนขยายเป็น C #

การใช้งาน:

Uri uri = new Uri("http://www.somewhere.com?param1=foo&param2=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);
        }
    }
}


1

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



0

ฉันได้สร้างส่วนขยายอย่างง่ายเนื่องจากคำตอบอื่น ๆ สองสามข้อนั้นไม่มีข้อยกเว้นหากไม่มีการ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() 

-1

ตัวอย่างง่ายๆจะใช้สตริงย่อยเช่น:

string your_url = "http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye";
string path_you_want = your_url .Substring(0, your_url .IndexOf("?"));



โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.