2012-04-20 5 views
0

URL 재 작성을 사용했습니다. 나는 한 가지 질문이 있습니다. URL이 비슷하다는 것입니다.C#을 사용하여 UrlRewriting에서 브라우저의 전체 URL을 얻는 방법

이 URL은 겹쳐 쓰므로이 전체 URL을 작성하면됩니다.

내가 url 변수에 /learnmore.aspx있어이 코드 후
string url=Request.RawUrl; 

하지만 내가 원하는 전체 URL http://localhost/learnmore.aspx

내가 어떻게 할 수 있습니까?

+0

돈 ' 이것이 URL 재 작성과 어떻게 관련되는지 보지 않으시겠습니까? Request.Url.AbsoluteUri? –

+0

네,하지만 Request.Url.AbsoluteUri를 사용하면 http : //localhost/Default.aspx? pageid = 25가되어 URL 재 작성으로 인해 저에게 유용하지 않습니다. –

답변

1

당신은 호스트와 계획이 방법으로 얻을 수

http://localhost/

을 한 다음 추가 할 수 있습니다 RawUrl

0

당신이 기본 URL 얻기 위해 시도 할 수 있습니다 : 쿼리 문자열 항목을 제거하려면 :

string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + 
    Request.ApplicationPath.TrimEnd('/') + "/"; 
3
string url = HttpContext.Current.Request.Url.AbsoluteUri; 
// http://localhost/learnmore.aspx 

string path = HttpContext.Current.Request.Url.AbsolutePath; 
// /localhost/learnmore.aspx 

string host = HttpContext.Current.Request.Url.Host; 
// localhost 

편집을

var uri = new Uri(HttpContext.Current.Request.Url.AbsoluteUri); 
string path = uri.GetLeftPart(UriPartial.Path); 

또는

Uri url = new Uri("http://www.somesite.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye"); 
string path = String.Format("{0}{1}{2}{3}", url.Scheme, 
    Uri.SchemeDelimiter, url.Authority, url.AbsolutePath); 
을 ( Get url without querystring에서 발견)

또는

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

여기 URLRewritting을 사용 했으므로 위의 세 가지를 사용하면 다음과 같은 결과가 나옵니다. http : //localhost/Default.aspx? pageid = 25하지만 http : //localhost/learnmore.aspx가 필요합니다. –

+0

@KartikPatel, 답변을 편집했습니다. 그냥 솔루션을 지금 시험해보십시오 – Habib

+0

감사합니다. Habib ... 이제 잘 작동합니다 ... –

0

Request.Url.AbsoluteUri 트릭을 수행합니다. 이와

Request.Url.GetLeftPart(UriPartial.Authority) 

당신이 얻을 것이다 :

관련 문제