2012-10-09 2 views
3

MVC 프로젝트를 배포하는 동안 상대 경로 w.r.t 서버 문제가 발생했습니다. IIS에서 응용 프로그램으로 프로젝트를 호스팅하고 있습니다. 마지막으로 내 응용 프로그램의 URL은 http://localhost/portal/Account/Login입니다. 여기서 'portal'은 IIS의 응용 프로그램 이름입니다. ASP.net 개발 서버에서 모든 것이 잘 작동하고있었습니다. 배포하는 동안 서버와 관련된 상대 경로가 필요했습니다. 이 때문에 내 jquery 아약스 요청이 실패하기 시작했습니다. 이 문제를 해결하기 위해 숨겨진 필드에서 작업을 수행하고 거기에서 액세스하여 아약스 요청에 사용합니다. 다음은 코드입니다.MVC 배포 - 응용 프로그램 경로와 관련된 상대 경로 문제

<input type="hidden" value="@Url.Action("GetNewOffersSearch", "Updates")" id="NewOffersSearchUrl" /> 

var NewoffersUrl = document.getElementById("NewOffersSearchUrl").value; 


$.ajax({ 
      type: 'GET', 
      url: NewoffersUrl , 
      cache: false, 
      timeout: 10000, 
      contentType: "application/json; charset=utf-8", 
      success: function (_results) { 
       $("#updatesContent").html(_results); 
      }, 
      error: function (_results) { 

      } 
     }); 

처음에는 NewoffersUrl이 "/Updates/GetNewOffersSearch"이고 경로 오류가 발생했습니다. 하지만 지금은 "/portal/Updates/GetNewOffersSearch"이며 그 작동 괜찮아요

나는 단지 다음과 같은 접근법을 알고 싶습니다 올바른지. 이 문제에 대한 더 나은 수정이 있습니까?

답변

3

AJAX 요청은 비슷하지만 숨겨진 필드를 사용하는 대신 URL을 ajax 호출의 url 매개 변수에 직접 전달합니다.

$.ajax({ 
     type: 'GET', 
     url: @Url.Action("GetNewOffersSearch", "Updates"), 
     cache: false, 
     timeout: 10000, 
     contentType: "application/json; charset=utf-8", 
     success: function (_results) { 
      $("#updatesContent").html(_results); 
     }, 
     error: function (_results) { 

     } 
    }); 
+0

예. 좋은 생각입니다. – amesh

+0

잘 작동합니다. 그러나 Visual Studio intellisense는 그것을 특별한 것으로 발견합니다. – amesh

+0

amesh - '@ the Url.Action'을 사용해야 할 수 있습니다. 예 : '@ Url.Action ("GetNewOffersSearch", "Updates")', –

관련 문제