0
我必須從另一個網站獲取html響應並加載到我的應用程序。我下面寫的代碼,如何在ASP.net MVC中實現動態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路徑映射到相對路徑
/xxxx/yyyy/dojo.js ,
/xxxx/style/logon.css,
/xxxx/images/logon.png
在HTML
,在這裏,我要改變,要像下面實際的URL
http://localhost/xxxx/yyyy/dojo.js ,
http://localhost/xxxx/style/logon.js ,
http://localhost/xxxx/images/logon.png
一個選項是在html中找到這些內容取代。
是否有任何其他選項可以動態更改網址? IIS URL重寫模塊是否適合我的要求?
請分享你的想法
感謝@JesseJhonson。但我在js文件中有一些ajax web服務調用,我需要替換它。我不知道未來會有多少服務電話?還有其他解決方案嗎? –
我使用上面的相同方法來替換鏈接的CSS文件和JS腳本中的絕對URL。你有沒有考慮使用反向代理?像[Titanium-Web-Proxy](https://github.com/justcoding121/Titanium-Web-Proxy)可能會簡化這個過程。本質上,你解析HTML並將所有URLs替換爲指向你的反向代理(類似於mydomain.com/proxy.ashx?uri={original url})。通過這種方式,所有外部文件(js,css等)都將通過代理加載,您可以通過代理獲取內容,解析,修改和提供與HTML相同的服務。 –