积极答复者
url重写时遇到问题

问题
-
我自定义了一个XML文件来解析传入的URL。
在一个继承至 IHttpModule 的HtmlModules类的Init方法添加的BeginRequest事件,在这个事件处理程序中进行URL解析。例如传入的URL是:\Default_id_5.aspx 解析成了\Default.aspx?id=5。然后使用HttpContext.Current.RewritePath(sendTo);(sendTo = \Default.aspx?id=5) 重写URL。我在vs2008中进行调试。但是呈现的页面中什么也没有。本来应该有内容的。我在Default.aspx页面中的Page_Load事件中添加断点。断点也没命中。为什么会这样?我感觉这个页面没有被解析。要怎么才能让得到解析呢?
郭鹏
答案
-
您好,web.config配置问题:
应配置在httpModules中,您出示的是httpHandlers的配置,两者不是同一个东西
<httpModules>
<add name="Test" type="Test.Page.HtmlHandler,Test.Page"/>
</httpModules>
同时,其中请注意:type="Test.Page.HtmlHandler,Test.Page" type的字符串为两部分,被逗号隔开,逗号前面是类的完全名称,后面是程序集名,不是命名空间。请确定您的程序集名是Test.Page。- 已标记为答案 Hugo12345 2009年8月7日 14:56
全部回复
-
urlrewrite通常是通过绑定HttpApplication 的 AuthorizeRequest 等事件来实现。
虽然HttpModule只在网站的第一次访问时才触发,但事件将在每次请求中被触发。
例如:
public void Init(HttpApplication context)
{
context.AuthenticateRequest += new EventHandler(URLRewriter);
}protected void URLRewriter(object sender, EventArgs e)
{
if (HttpContext.Current.Request.FilePath.IndexOf("abc.aspx") != -1)
{
HttpContext.Current.RewritePath("/SalesOrder/WebForm2.aspx");}
}
所有访问abc.aspx的都将被重写到SalesOrder/WebForm2.aspx- 已编辑 JiyuanModerator 2009年8月7日 13:12
-
代码如下:
public class HtmlModules : IHttpModule { #region IHttpModule 成员 public void Dispose() { } public void Init(HttpApplication context) { // context.BeginRequest += new EventHandler(context_BeginRequest); context.AuthenticateRequest += new EventHandler(context_AuthenticateRequest); } void context_AuthenticateRequest(object sender, EventArgs e) { SinoSpace.RewriteSupport.UrlRules url = null; HttpApplication app = (HttpApplication)sender; url = SinoSpace.RewriteSupport.UrlRewriteConfiguration. FindWriteTo(HttpContext.Current.Request.RawUrl); string sendTo = url.WriteTo; //sendTo = /default.aspx?id=5 HttpContext.Current.RewritePath(sendTo); }
然后webconfig 配置:
<add verb="*" path="*.aspx" validate="false" type="Test.Page.HtmlHandler, Test.Page"/>
vs调试default.aspx 仍然没有东西
郭鹏 -
您好,web.config配置问题:
应配置在httpModules中,您出示的是httpHandlers的配置,两者不是同一个东西
<httpModules>
<add name="Test" type="Test.Page.HtmlHandler,Test.Page"/>
</httpModules>
同时,其中请注意:type="Test.Page.HtmlHandler,Test.Page" type的字符串为两部分,被逗号隔开,逗号前面是类的完全名称,后面是程序集名,不是命名空间。请确定您的程序集名是Test.Page。- 已标记为答案 Hugo12345 2009年8月7日 14:56