我们为此使用了自定义 404 处理程序。ASP.NET 网站将处理该请求,而不是返回 404,然后将在内容站点上查找相同的路径。如果内容存在,我们将返回该页面。如果不是,我们返回 404 错误。
MVC 示例
[DefaultAction("index")]
public class RescuesController : SmartDispatchController
{
private static readonly Regex headtitleRegex = new Regex("<h1>([^<]*)</>>");
public RescuesController(IApplicationContextProvider currentCustomerProvider, IConfigurationManager configurationManager) : base(currentCustomerProvider, configurationManager) { }
public void Index()
{
RenderView("rescues","content");
string pageName = Request.Uri.LocalPath;
if (pageName.EndsWith("/index"))
pageName = pageName.Substring(0, pageName.LastIndexOf("/index"));
try
{
var remoteContent = GetRemoteContent(pageName);
var matches = headtitleRegex.Match(remoteContent);
if (matches.Groups.Count > 1)
PropertyBag.HeadTitle = matches.Groups[1].Captures[0].Value;
PropertyBag.remoteContent = headtitleRegex.Replace(remoteContent, "");
} catch (WebException) {
Handle404();
}
}
public static string GetRemoteContent(string pageName)
{
return (new WebClient()).DownloadString("http://content.mysite.com/" + pageName);
}
}
http://www.robertgray.net.au/posts/2010/4/404-handlers