IIS设置:站点右键属性,自定义错误,找到404,修改为url,将url设置成/Default.aspx
测试一下:随便敲个地址,比如:http://localhost/123.html,应该跳转到Default.aspx
然后在Default.aspx中写如下逻辑:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.RawUrl.IndexOf(".html") == -1)
{
return;
}
string filePath = Server.MapPath("/");
string[] temp = Request.RawUrl.Split('/');
string htmlFile = temp[temp.Length - 1];
string aspxFile = htmlFile.Replace(".html", ".aspx");
bool b = false;
try
{
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("http://" + Request.Url.Host + ":" + Request.Url.Port.ToString() + "/" + aspxFile);
httpWebRequest.Method = "GET";
using (Stream stream = httpWebRequest.GetResponse().GetResponseStream())
{
using (StreamReader streamReader = new StreamReader(stream, Encoding.UTF8))
{
FileStream fileStream = new FileStream(filePath + htmlFile, FileMode.Create);
byte[] contents = Encoding.Default.GetBytes(streamReader.ReadToEnd());
fileStream.Write(contents, 0, contents.Length);
fileStream.Close();
b = true;
}
}
}
catch (Exception exception)
{
Response.Write(exception.ToString());
}
finally
{
}
if (b)
{
Server.Transfer(htmlFile, true);
}
}
大概意思是,如果输入的是index.html,那么就会读取index.aspx的动态内容,然后创建index.html
这样第二次访问http://localhost/index.html,就是访问静态页了,然后再做个反向代理(nginx)呗