/*
* 1、新建类,继承 System.Web.IHttpModule,实现接口
* 2、创建xml文件,记录文件真实名称(aspx)跟显示名称(html)
* 3、web.config 跟节点下面添加 配置
*/
/* XML 文件
<?xml version="1.0" encoding="utf-8" ?>
<Urls>
<Url htmlUrl="abc.html" realUrl="abc.aspx" />
</Urls>
*/
/* web.config 配置 跟节点下面添加
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="ReurlModule" type="JingXiaMis.App.HttpModule, JingXiaMis.App" />
</modules>
</system.webServer>
*/
public class HttpModule : System.Web.IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(ReUrl_BeginRequest);
}
private void ReUrl_BeginRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
string requestPath = context.Request.Path;//获得当前页面(含目录)
List<PageUrls> urlList = GetPageUrls();
if (urlList.Any())
{
string htmlPageName = Path.GetFileName(requestPath);
var thisModel = urlList.Find(d => d.HtmlUrl == htmlPageName);
if (thisModel != null)
{
string realPageUrl = requestPath.Replace(htmlPageName, thisModel.RealUrl);
//重写文件的真实路径
context.RewritePath(realPageUrl);
}
}
}
// 读取XML 文件信息
private List<PageUrls> GetPageUrls()
{
List<PageUrls> list = new List<PageUrls>();
XMLHelper xmlHelper = new XMLHelper();
string xmlPath = "/Config/PageUrlsXML.xml";
var data = xmlHelper.GetDataSetByXml(xmlPath);
if (data.Tables.Count > 0)
{
list = ListHelper.DtConvertToModel<PageUrls>(data.Tables[0]).ToList();
}
return list;
}
}
C# aspx页面地址栏显示为 html
最新推荐文章于 2021-06-17 06:50:16 发布