C #에서 현재 페이지의 URL을 얻는 방법 [중복]
이 질문에 이미 답변이 있습니다.
누구든지 C #에서 ASP.NET의 현재 작업 페이지 URL을 얻는 데 도움을 줄 수 있습니까?
이 시도 :
string url = HttpContext.Current.Request.Url.AbsoluteUri;
// http://localhost:1302/TESTERS/Default6.aspx
string path = HttpContext.Current.Request.Url.AbsolutePath;
// /TESTERS/Default6.aspx
string host = HttpContext.Current.Request.Url.Host;
// localhost
때때로 URL에서 다른 값을 가져와야 할 수 있습니다.
아래 예는 URL의 다른 부분을 추출하는 다양한 방법을 보여줍니다.
예 : (샘플 URL)
http://localhost:60527/WebSite1test/Default2.aspx?QueryString1=1&QueryString2=2
암호
Response.Write("<br/> " + HttpContext.Current.Request.Url.Host);
Response.Write("<br/> " + HttpContext.Current.Request.Url.Authority);
Response.Write("<br/> " + HttpContext.Current.Request.Url.Port);
Response.Write("<br/> " + HttpContext.Current.Request.Url.AbsolutePath);
Response.Write("<br/> " + HttpContext.Current.Request.ApplicationPath);
Response.Write("<br/> " + HttpContext.Current.Request.Url.AbsoluteUri);
Response.Write("<br/> " + HttpContext.Current.Request.Url.PathAndQuery);
산출
localhost
localhost:60527
60527
/WebSite1test/Default2.aspx
/WebSite1test
http://localhost:60527/WebSite1test/Default2.aspx?QueryString1=1&QueryString1=2
/WebSite1test/Default2.aspx?QueryString1=1&QueryString2=2
위의 샘플 코드를 복사하여 다른 URL로 asp.net 웹 양식 응용 프로그램에서 실행할 수 있습니다.
또한 ASP 라우팅을 사용할 수있는 경우 ASP.Net 라우팅을 읽는 것이 좋습니다. 그러면 쿼리 문자열에 기존 URL을 사용할 필요가 없습니다.
http://msdn.microsoft.com/en-us/library/cc668201%28v=vs.100%29.aspx
Canavar의 게시물 덕분에 이것이 내 솔루션이므로 공유했습니다.
다음과 같은 것이 있다면 :
"http://localhost:1234/Default.aspx?un=asdf&somethingelse=fdsa"
또는 다음과 같이 :
"https://www.something.com/index.html?a=123&b=4567"
사용자가 입력하는 부분 만 원하면 다음과 같이 작동합니다.
String strPathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
String strUrl = HttpContext.Current.Request.Url.AbsoluteUri.Replace(strPathAndQuery, "/");
결과는 다음과 같습니다.
"http://localhost:1234/"
"https://www.something.com/"
http : //와 첫 번째 슬래시 사이의 부분 만 원하는 경우
string url = Request.Url.Host;
이 페이지에서 호출되면 stackoverflow.com을 반환합니다.
여기에 완전한 분석이 있습니다.
request.rawurl은 현재 페이지의 콘텐츠를 제공하여 필요한 정확한 경로를 제공합니다.
사용하다 HttpContext.Current.Request.RawUrl
당신이 얻고 싶다면
localhost:2806
...에서
http://localhost:2806/Pages/
그런 다음 다음을 사용하십시오.
HttpContext.Current.Request.Url.Authority
global.asax 파일의 경로 / URL이 필요한 사람들을위한 팁;
global.asax> Application_Start 에서 실행해야하고 앱 풀 모드가 통합 된 경우 아래 오류가 표시됩니다.
Application_Start의이 컨텍스트 예외에서 요청을 사용할 수 없습니다.
이 경우 다음을 사용해야합니다.
System.Web.HttpRuntime.AppDomainAppVirtualPath
Hope will help others..
A search landed me at this page, but it wasn't quite what I was looking for. Posting here in case someone else looking for what I was lands at this page too.
There is two ways to do it if you only have a string value.
.NET way:
Same as @Canavar, but you can instantiate a new Uri Object
String URL = "http://localhost:1302/TESTERS/Default6.aspx";
System.Uri uri = new System.Uri(URL);
which means you can use the same methods, e.g.
string url = uri.AbsoluteUri;
// http://localhost:1302/TESTERS/Default6.aspx
string host = uri.host
// localhost
Regex way:
Getting parts of a URL (Regex)
I guess its enough to return absolute path..
Path.GetFileName( Request.Url.AbsolutePath )
using System.IO;
참고URL : https://stackoverflow.com/questions/593709/how-to-get-the-url-of-the-current-page-in-c-sharp
'developer tip' 카테고리의 다른 글
SQL Server의 테이블에서 열 이름을 어떻게 가져올 수 있습니까? (0) | 2020.10.02 |
---|---|
PDO 준비 문이 SQL 주입을 방지하기에 충분합니까? (0) | 2020.10.02 |
IE에서 개발자 도구를 한 번만 연 후에 만 JavaScript가 작동하는 이유는 무엇입니까? (0) | 2020.10.02 |
jQuery에게 무언가를 실행하기 전에 모든 이미지가로드 될 때까지 기다리도록 요청하는 공식적인 방법 (0) | 2020.10.02 |
jQuery로 요소에 대한 클래스 목록 가져 오기 (0) | 2020.10.02 |