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相同的服务。 –