querystring없이 URL 가져 오기
다음과 같은 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의 제안을 반영하도록 첫 번째 솔루션 수정.
더 간단한 해결책은 다음과 같습니다.
var uri = new Uri("http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye");
string path = uri.GetLeftPart(UriPartial.Path);
여기에서 빌린 : 쿼리 문자열 자르기 및 깨끗한 URL 반환 C # ASP.net
이것은 내 솔루션입니다.
Request.Url.AbsoluteUri.Replace(Request.Url.Query, String.Empty);
Request.RawUrl.Split(new[] {'?'})[0];
좋은 대답은 여기 대답의 소스
Request.Url.GetLeftPart(UriPartial.Path)
내 길 :
new UriBuilder(url) { Query = string.Empty }.ToString()
또는
new UriBuilder(url) { Query = string.Empty }.Uri
Request.Url.AbsolutePath
페이지 이름과 Request.Url.Authority
호스트 이름 및 포트 를 얻는 데 사용할 수 있습니다 . 나는 당신이 원하는 것을 정확하게 줄 수있는 빌트인 속성이 있다고 생각하지 않지만 직접 결합 할 수 있습니다.
@Kolman의 답변을 사용하는 확장 방법이 있습니다. GetLeftPart보다 Path ()를 사용하는 것이 조금 더 쉽습니다. 적어도 C #에 확장 속성을 추가 할 때까지 Path의 경로를 GetPath로 바꾸는 것이 좋습니다.
용법:
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);
}
}
}
Split () 변형
참고로이 변형을 추가하고 싶습니다. URL은 종종 문자열이므로 Split()
보다 방법 을 사용하는 것이 더 간단합니다 Uri.GetLeftPart()
. 그리고 Split()
Uri는 예외를 던지는 반면 상대, 빈 및 null 값으로 작동하도록 만들 수도 있습니다. 또한 Urls에는 /report.pdf#page=10
(해당 페이지에서 pdf를 여는) 해시가 포함될 수도 있습니다 .
다음 방법은 이러한 모든 유형의 Urls를 처리합니다.
var path = (url ?? "").Split('?', '#')[0];
출력 예 :
- null ---> 비어 있음
- 비어 있음 ---> 비어 있음
- http : //domain/page.html ---> http : //domain/page.html
- http://domain/page.html?q=100 ---> http://domain/page.html
- http://domain/page.html?q=100#page=2 ---> http://domain/page.html
page.html ---> page.html
- page.html?q=100 ---> page.html
- page.html?q=100#page=2 ---> page.html
- page.html#hash ---> page.html
Request.RawUrl.Split('?')[0]
Just for url name only !!
Solution for Silverlight:
string path = HtmlPage.Document.DocumentUri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);
I've created a simple extension, as a few of the other answers threw null exceptions if there wasn't a QueryString
to start with:
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;
}
Usage:
var url = Request.Url?.AbsoluteUri.TrimQueryString()
string url = "http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye";
string path = url.split('?')[0];
simple example would be using substring like :
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();
System.Uri.GetComponents
, just specified components you want.
Uri uri = new Uri("http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye");
uri.GetComponents(UriComponents.SchemeAndServer | UriComponents.Path, UriFormat.UriEscaped);
Output:
http://www.example.com/mypage.aspx
Try this:
urlString=Request.RawUrl.ToString.Substring(0, Request.RawUrl.ToString.IndexOf("?"))
from this: http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye you'll get this: mypage.aspx
this.Request.RawUrl.Substring(0, this.Request.RawUrl.IndexOf('?'))
참고URL : https://stackoverflow.com/questions/4630249/get-url-without-querystring
'Programing' 카테고리의 다른 글
Objective-C에서 NSArray를 NSString으로 변환 (0) | 2020.05.17 |
---|---|
발리 타임 아웃 시간 변경 (0) | 2020.05.17 |
R에서 루프 작동 속도 향상 (0) | 2020.05.17 |
JSON 문자열을 만들 때 특수 문자를 이스케이프 처리하는 방법은 무엇입니까? (0) | 2020.05.17 |
마우스가 테이블의 행을 넘어갈 때 손으로 커서 변경 (0) | 2020.05.17 |