2011-01-07 17 views
147

Ho un URL simile a questo:Get URL senza querystring

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

voglio ottenere http://www.example.com/mypage.aspx da esso.

Puoi dirmi come posso ottenerlo?

risposta

112

È possibile utilizzare 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); 

Oppure si può utilizzare substring

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

EDIT: Modificare la prima soluzione per riflettere suggerimento di brillyfresh nei commenti.

+6

url.AbsolutePath restituisce solo la parte del percorso dell'URL (/mypage.aspx); prepend url.Scheme (http) + Uri.SchemeDelimiter (: //) + url.Authority (www.somesite.com) per l'URL completo che si desidera – Ryan

+16

Il metodo Uri.GetLeftPart è più semplice come accennato http://stackoverflow.com/questions/1188096/truncating-query-string-return-clean-url-c-sharp-asp-net/1188180 # 1188180 –

10

È possibile utilizzare Request.Url.AbsolutePath per ottenere il nome della pagina e Request.Url.Authority per il nome host e la porta. Non credo che ci sia una proprietà integrata per darti esattamente quello che vuoi, ma puoi combinarli tu stesso.

+1

E mi sta dando /mypage.aspx, non quello che volevo. –

307

Ecco una soluzione più semplice:

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

preso in prestito da qui: Truncating Query String & Returning Clean URL C# ASP.net

+23

Questa dovrebbe essere la risposta accettata. –

+9

Versione a una riga: 'return Request.Url.GetLeftPart (UriPartial.Path);' – jp2code

+0

'uri.GetComponent (' è un altro metodo fantastico per ottenere parti di un Uri. Non sapevo di questi due fino ad ora! – AaronLS

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

Semplicemente e funziona con gli URL senza "?" – rkawano

+1

Mi piace solo per il fatto che puoi usarlo senza un uri completo. – KingOfHypocrites

33

Questa è la mia soluzione:

Request.Url.AbsoluteUri.Replace(Request.Url.Query, String.Empty); 
+4

Questo darà errore se non ci sono stringhe di query –

11

Il mio modo:

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

o

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

Questo è quello che uso per il progetto NET Core 1.0 perché non ha metodo 'Uri.GetLeftPart'. L'ultima versione di NET Core (1.1) dovrebbe avere quel metodo (non posso confermare perché non sono sul core 1.1 di rete per ora) – psulek

+0

Mi piace questo perché costruire URI è esattamente ciò che è per UriBuilder. Tutte le altre risposte sono (buone) hack. –

3

Ecco un metodo di estensione utilizzando @ risposta di Kolman. È leggermente più facile ricordare di usare Path() di GetLeftPart. Potresti voler rinominare Path to GetPath, almeno fino a quando non aggiungono proprietà di estensione a C#.

Usage:

Uri uri = new Uri("http://www.somewhere.com?param1=foo&param2=bar"); 
string path = uri.Path(); 

La classe:

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

Request.RawUrl.Split ('?') [0]

Solo per solo il nome url !!

-1

this.Request.RawUrl.Substring (0, this.Request.RawUrl.IndexOf ('?'))

25

Buona risposta trova anche qui source of answer

Request.Url.GetLeftPart(UriPartial.Path) 
0

Soluzione per Silverlight:

string path = HtmlPage.Document.DocumentUri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);