2016-08-24 2 views
0

다른 웹 사이트에서 HTML 응답을 받고 응용 프로그램에로드해야합니다.ASP.net MVC에서 동적 URL 리디렉션을 수행하는 방법?

HTML에서
/xxxx/yyyy/dojo.js , 
    /xxxx/style/logon.css, 
    /xxxx/images/logon.png 

, 여기에 내가 다음과 같은 실제 URL로 그를 변경해야 내가

using System; 
    using System.Collections.Generic; 
    using System.IO; 
    using System.Linq; 
    using System.Net; 
    using System.Web; 
    using System.Web.Mvc; 

    namespace MVC_TEST.Controllers 
    { 
     public class DocumentCloudController : Controller 
     { 
      public string Index() 
      { 
       var result = GetResponse(); 
       return result; 
      } 

      private static string GetResponse() 
      { 

       var html = string.Empty; 
       const string url = @"http://localhost/xxxxx/yyyyy/logon.aspx"; 

       var request = (HttpWebRequest)WebRequest.Create(url); 
       request.AutomaticDecompression = DecompressionMethods.GZip; 
       using (var response = (HttpWebResponse)request.GetResponse()) 
       { 
        using (var stream = response.GetResponseStream()) 
        { 
         if (stream != null) 
         { 
          using (var reader = new StreamReader(stream)) 
          { 
           html = reader.ReadToEnd(); 
          } 
         } 
        } 
       } 


       return html; 
      }} 


     } 
    } 

컨트롤이 제대로로드, 코드 아래에 작성하지만 이미지, CSS와 JS 경로는 상대 경로에 매핑

http://localhost/xxxx/yyyy/dojo.js , 
http://localhost/xxxx/style/logon.js , 
http://localhost/xxxx/images/logon.png  

하나의 옵션은 이러한 내용을 html로 대체합니다.

동적으로 URL을 변경할 수있는 다른 옵션이 있습니까? IIS URL 재 작성 모듈이 필자의 요구 사항에 적합합니까?

일할 수있는 IIS URL 재 작성 모듈을 사용하여 자신의 생각

답변

1

을 공유하지만 난 쿼리하고 DOM을 조작 할 HTML Agility Pack 또는 AngleSharp 같은 HTML 파서를 사용하는 것이 좋습니다 것이다.

foreach (var link in document.DocumentNode.SelectNodes("//link[@href]")) 
{ 
    var orgHrefValue = link.GetAttributeValue("href", string.Empty); 
    var updHrefValue = string.Concat("[BASE URL]", GetAbsoluteUrlString(requestedUrl, orgHrefValue).AbsoluteUri); 
    link.SetAttributeValue("href", updHrefValue); 
} 

private static Uri GetAbsoluteUrlString(string baseUrl, string url) 
{ 
    var uri = new Uri(url, UriKind.RelativeOrAbsolute); 

    if (!uri.IsAbsoluteUri) 
     uri = new Uri(new Uri(baseUrl), uri); 

    return uri; 
} 
+0

감사 @JesseJhonson :

아래의 예는 역방향 프록시를 만들 때 나를 위해 일한 조각입니다. 하지만 js 파일에서 일부 아약스 webservice 호출을 가지고, 나는 그것을 대체해야합니다. 나는 앞으로 얼마나 많은 서비스 요청이 올지 알지 못한다. 다른 해결책이 있습니까? –

+0

링크 된 CSS 파일과 JS 스크립트에서 절대 URL을 바꾸기 위해 위의 동일한 방법을 사용했습니다. 당신은 역방향 프록시를 사용하여 봤어? [Titanium-Web-Proxy] (https://github.com/justcoding121/Titanium-Web-Proxy)와 같은 것이 프로세스를 단순화 할 수 있습니다. 본질적으로 HTML을 구문 분석하고 대신 모든 URL을 바꿔 리버스 프록시 (mydomain.com/proxy.ashx?uri={original URL과 같은 것)를 가리 킵니다. 이렇게하면 모든 외부 파일 (js, css 등)이 프록시를 통해로드되어 HTML을 사용하는 것과 같은 방식으로 내용을 파싱, 구문 분석, 수정 및 제공 할 수 있습니다. –

관련 문제