??? URL 重写是截取传入 Web 请求并自动将请求重定向到其他 URL 的过程。 比如浏览器发来请求hostname/101.aspx ,服务器自动将这个请求中定向为http://hostname/list.aspx?id=101。
url重写的优点在于: 缩短url,隐藏实际路径提高安全性 易于用户记忆和键入。 易于被搜索引擎收录
二 实现url重写的基本方法 下载MS的URLRewriter.dll,放到你的web程序的bin下 下载地址1:http://www.sinoec.cn/fordown/URLRewriter.dll 下载地址2:download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi
下载完成后,在web.config里设置如下:
<?xml version="1.0" encoding="utf-8" ?> <!--overred--> <configuration> <configSections> <section name="RewriterConfig"type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" /> </configSections> <RewriterConfig> <Rules> <RewriterRule> <LookFor>~/d(/d+)/.aspx</LookFor> <SendTo>~/default.aspx?id=$1</SendTo> </RewriterRule> </Rules> </RewriterConfig> <system.web> <httpHandlers> <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" /> </httpHandlers> </system.web> </configuration>
其中 <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
用于指定配置节"RewriterConfig"的处理程序类的名称为”URLRewriter.Config.RewriterConfigSerializerSectionHandler”,该类存在于bin目录下的URLRewriter .dll文件中
关键的是这两句 <LookFor>~/d(/d+)/.aspx</LookFor> <SendTo>~/default.aspx?id=$1</SendTo>
< LookFor>~/d(/d+)/.aspx</LookFor>表示,用户输入的url,d(/d+)/.aspx是 url中文件名匹配的正则表达式(此处为字母d开头,后面跟一个或多个数字,并以.aspx结尾。用户也可根据自己的需要自行设定)。 <SendTo> ~/default.aspx?id=$1</SendTo>,表示当服务器接收到符合上面条件的请求后如何重写url。此处表示访问 defalutl.aspx并传入参数id,其值$1将用用户请求的文件名中的第一个数字来表示。 例如用户输入 hostname/d11.aspx,服务器会把他重写为http://hostname/default.aspx?id=11。换句话说用户输入 http: //hostname/d11.aspx,实际访问的是http://hostname/default.aspx?id=11。这样就起到了隐藏真实文件名,并便于用户记忆的作用。 iis注册:如果要实现.html 的伪静态页面,就要进行IIS配置。要选择ASPX进行编辑,图有错。

还有第八步中的”untick Verify that file exists.“要看仔细。否则重写HTML就无效。
4:备注
不要在"应用程序扩展"里“添加" .html的映射,否则正常的html将无法访问。 - 打开Internet信息服务( IIS )管理器
click to expand 点击展开 - Right-click on the website where you want to configure UrlRewriter.NET and select Properties.右点击这个网站,只要你想配置urlrewriter.net并选择属性。
click to expand 点击展开 - Click on the Home Directory tab.点击home目录选项卡。
click to expand 点击展开 - Click the Configuration button.点击配置按钮。
click to expand 点击展开 - Select the .asax application extension and click Edit…选择。 asax申请延期,并单击编辑…
click to expand 点击展开 - Copy the Executable path (…/aspnet_isapi.dll) to the clipboard and click Cancel.拷贝可执行文件路径( … … / aspnet_isapi.dll )到剪贴板,然后点击取消。
click to expand 点击展开 - Click Insert…点击插入…
click to expand 点击展开 - Paste the Executable path (…/aspnet_isapi.dll) from the clipboard, untick Verify that file exists.糊一个可执行文件路径( … … / aspnet_isapi.dll )由剪贴板, untick验证文件是否存在。
click to expand 点击展开 - Click OK to close the Add/Edit Application Extension Mapping dialog单击确定关闭添加/编辑应用扩展映射对话框
click to expand 点击展开 - Click OK to close the Application Configuration dialog.单击确定关闭该应用程序配置对话框。
? 处理回发 在重写后的url里如果产生回发,例如有一个按钮,又调用了该被重写的 aspx,用户浏览器中将会显示该aspx文件实际的地址,也就是http: //hostname/default.aspx?id=11。但从用户的角度考虑,如 果单击按钮时突然看到 URL 更改会使他们感到不安。因此必须解决这个问题。 解决方法有二: (1)自己定义一个Actionlessform类,在aspx中不再使用系统提供的form 标记
namespace ActionlessForm { public class Form : System.Web.UI.HtmlControls.HtmlForm { protected override void RenderAttributes(HtmlTextWriter writer) { writer.WriteAttribute("name", this.Name); base.Attributes.Remove("name"); writer.WriteAttribute("method", this.Method); base.Attributes.Remove("method"); this.Attributes.Render(writer); base.Attributes.Remove("action"); if (base.ID != null) writer.WriteAttribute("id", base.ClientID); } } }
创建此类并对其进行编译之后,要在 ASP.NET Web 应用程序中使用它,应首先将其添加到 Web 应用程序的 References 文件夹中。然后,要使用它来代替 HtmlForm 类,做法是在 ASP.NET 网页的顶部添加以下内容:
<%@ Register TagPrefix="skm" Namespace="ActionlessForm" Assembly="ActionlessForm" %> 然后,将 <form runat="server">(如果有)替换为:<skm:Form id="Form1" method="post" runat="server"> 并将右边的 </form> 标记替换为:</skm:Form>
个人并不推荐该方法 (2)第二种方法就是继承page,这样你不需要在aspx页中改任何东西。 代码: using System; using System.IO; using System.Web; using System.Web.UI; namespace URL { public class OLPage : Page { public OLPage() {} protected override void Render(HtmlTextWriter writer) { if (writer is System.Web.UI.Html32TextWriter) { writer = new FormFixerHtml32TextWriter(writer.InnerWriter); } else { writer = new FormFixerHtmlTextWriter(writer.InnerWriter); } base.Render(writer); } }
internal class FormFixerHtml32TextWriter : System.Web.UI.Html32TextWriter { private string _url; // 假的URL
internal FormFixerHtml32TextWriter(TextWriter writer):base(writer) { _url = HttpContext.Current.Request.RawUrl; }
public override void WriteAttribute(string name, string value, bool encode) { if (_url != null && string.Compare(name, "action", true) == 0) { value = _url; } base.WriteAttribute(name, value, encode);
} }
internal class FormFixerHtmlTextWriter : System.Web.UI.HtmlTextWriter { private string _url; internal FormFixerHtmlTextWriter(TextWriter writer):base(writer) { _url = HttpContext.Current.Request.RawUrl; }
public override void WriteAttribute(string name, string value, bool encode) { if (_url != null && string.Compare(name, "action", true) == 0) { value = _url; } base.WriteAttribute(name, value, encode); } }
}
把这个文件编译成dll,并在你的项目中引用它。
然后把项目中的所有aspx文件对应的cs文件中的继承page类的代码改写为继承OLPage。 例如 public class WebForm1:page 改写为 public class WebForm1:URL.OLPage
这样就解决回发问题。 编译*.dll方法:/t:library name.cs ? 二、关于RSS RSS是用于RSS阅读器的一种文件格式,也可以叫新闻种子,RSS种子等等。RSS是一个XML文件,后缀名可以是.aspx,但内容必须是Xml的。他的语法如下: ------------------- 一个RSS种子.xml -------------------------------- <?xml version="1.0" encoding="gb2312"?> <rss version="2.0"> <channel> <title>这个RSS种子(频道)的名称、标题</title> <link>这个RSS种子(频道)的连接地址.xml </link> <description>这个RSS种子(频道)的简要说明,随你怎么写...</description> <item> <title>具体的引用标题,比如一条新闻的标题</title> <link>http://www.该条新闻的连接地址.com/真实的html或者aspx地址.aspx?aaa=1&bbb=2</link> <description>该条新闻的介绍,一般是显示一部分的新闻内容...</description> <pubDate>这个不用说了,发布日期</pubDate> </item> </channel> </rss> --------------------------------------------- 为了让搜索引擎小蜘蛛知道这个种子在那里,你可以在<head>中加入一个<link>告诉它。 <head> ...... ...... <link rel="alternate" type="application/rss+xml" title="我的RSS标题" href="http://www.myweb.com/rss/myrss.xml"/> ...... ...... </head> ----------------------------------------------- 为了让那些使用RSS新闻阅读器的人可以订阅种子,你需要把这个文件连接到[RSS]的图片或一个(RSS)连接 三、关于OPML OPML文件其实就是RSS文件的目录集合,上面是一个RSS文件,而OPML是一大堆RSS文件的目录。 OPML文件也是XML格式的,他的语法如下: -------------- myopml.opml --------------------- <?xml version="1.0" encoding="gb2312"?> <opml version="1.0"> <head> <title>这个OPML目录文件的标题</title> <link>这个OPML目录文件的连接地址.opml</link> <description>这个OPML目录文件的简要说明,随你怎么写...</description> <head> <body> <!-- 一条 outline 记录一个RSS种子地址 --> <outline type="rss" title="RSS种子标题" text="RSS种子文本,跟标题差不多" description="RSS种子的简要说明" xmlUrl="RSS种子的连接地址.xml" htmlUrl="RSS种子的html连接地址.xml" /> ...... ...... </body> </opml> ------------------------------------------------------ 为了让搜索小蜘蛛知道OPML文件在那里,你可以在<head>中写一句<link>告诉它 <head> ... ... <link rel="outline" type="application/xml" title=这个OPML文件的标题" href="这个OPML文件的地址.opml"/> ... ... </head> |