기본 url 을 어떻게 다운로드합니까 javascript 를 사용하여?
E. g. 때는 내가 내 사이트 url 은 비주얼 스튜디오, 만약 내 ',' 에서 찾아보기의 http://localhost:20201/home/index 그녀의심장을 afaq http://localhost:20201 '나'
만약 내가 내 사이트 IIS 에서, 만약 내 가상 호스트 이름은 ',' 내가 디렉토리에는 미야프 및 url 은 http://localhost/MyApp/home/index 그녀의심장을 afaq 'http://localhost/MyApp'
'+' 찾았다 '로카션.프로토콜 사용하여 로카시옹스트로스나미' ('와' 로카시옹스트로스), 그 때 그 때 나는 내 사이트 작동합니다 찾아보기의 visual studio 를 통해 얻을 수 있지만, IIS, 호스트 it on / off '이라 이름' http://localhost 미야프 잘립니다.
Javascript 로 하고 대신 가치를 반드시 피해야 합니다 감지가 닷넷 (.net) 기반 코드. 사용자는 언제든지 실행할 수 있는 url 로 http://server/MyApp/MyApp/action like '위험' 이라는 이름을 알 수 없는 문제가 있는 컨트롤러, 어떤 경로를 프로그램.
In your 라이우 t.치타믈 파일 (또는 필요할 곳이라면 어디서든) 다음 코드를 추가합니다.
<script type="text/javascript">
window.applicationBaseUrl = @Html.Raw(HttpUtility.JavaScriptStringEncode(Url.Content("~/"), true));
alert(window.applicationBaseUrl + "asd.html");
// if you need to include host and port in the url, use this:
window.applicationBaseUrl = @Html.Raw(HttpUtility.JavaScriptStringEncode(
new Uri(
new Uri(this.Context.Request.Url.GetLeftPart(UriPartial.Authority)),
Url.Content("~/")
).ToString(), true));
alert(window.applicationBaseUrl + "asd.html");
</script>
새로운 Uri () '이' 부품 요구되는가 도왔으매 URL 은 항상 제대로 결합됨 (각 부품 시작되거나 끝나는 경우 직접 확인 없이 '/' 기호).
내가 생각하는 don& # 39, t, t # 39 이것은 가급적 doesn& JavaScript (클라이언트) 구축 및 it 환경에 대한 지식이 일부로 (미야프) 은 경로 정보 (마찬가지로 / home / 인덱스화할). 해결 방법으로 해석하는 경로 정보 (로카시옹스파스나미) 에 따라 도메인 또는 포트로부터 수도 있습니다.
그러나 JavaScript 의 변수로 글로벌 범위를 설정할 수 있습니다 (또는 어떤 판매업체에서 소송을 너회가) 를 포함하고 있는 패스 (path 는 서버에서 생성된 붙이십시오 꽂으십시오 variable).
뭐 그런 in your html 헤드 이 보일 수 있다.
<script type="text/javascript">
var global_baseurl = '<insert server side code that gets the path depending on your server side programming language>';
</script>
아래 코드를 시도하시겠습니까.
function getBaseURL() {
var url = location.href; // entire url including querystring - also: window.location.href;
var baseURL = url.substring(0, url.indexOf('/', 14));
if (baseURL.indexOf('http://localhost') != -1) {
// Base Url for localhost
var url = location.href; // window.location.href;
var pathname = location.pathname; // window.location.pathname;
var index1 = url.indexOf(pathname);
var index2 = url.indexOf("/", index1 + 1);
var baseLocalUrl = url.substr(0, index2);
return baseLocalUrl + "/";
}
else {
// Root Url for domain name
return baseURL + "/";
}
}
document.write(getBaseURL());
감사합니다.
시바
Net 핵심 변수로 사용하는 경우 얻을 수 있으며, javascript, url 설정 노드전역 범위 (예를 들어 있는 이 파일은 파일, 폴더 뷰 라이우 t.치타믈 / 공유):
<script type="text/javascript">
var url = '@string.Format("{0}://{1}{2}", Context.Request.Scheme, Context.Request.Host.Value , Url.Content("~/"))';
</script>
C # 에서 버전 6 과 보간은:
<script type="text/javascript">
var url = '@($"{Context.Request.Scheme}://{Context.Request.Host.Value}{Url.Content("~/")}")';
</script>
깨끗한 솔루션이므로 물거품이 될 것 같아요
<div id="BaseUrl" data-baseurl="@Context.Request.Url.GetLeftPart(UriPartial.Authority)@Url.Content("~/")"></div>
로컬 http://localhost:20201 iis 에서 부여하느뇨 http://localhost 있습니다
그럼 &solarisdvd Js :
var baseUrl = $("#BaseUrl").data("baseurl");
$.getJSON(baseUrl + "/Home/GetSomething", function (data) {
//Do something with the data
});
이아스파스 https://msdn.microsoft.com/en-us/library/system.uri.getleftpart (v = vs. 110) http://api.jquery.com/data/
@{ var baseHref = new System.UriBuilder(Request.Url.AbsoluteUri) { Path = Url.Content("~/") }; }
<base href="@baseHref" />
설명: