context.RewritePath在页面回发上不起作用 [英] context.RewritePath is not working on page post back

查看:85
本文介绍了context.RewritePath在页面回发上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们.
我在使用RewritePath方法重写url时遇到问题.

代码实现如下.



公共类重写:IHttpModule
{
公共rewrite()
{
//
//TODO:在此处添加构造函数逻辑
//
}
#region IHttpModule成员
公共无效Dispose()
{
//
}
公共无效Init(HttpApplication上下文)
{
context.BeginRequest + =新的EventHandler(context_BeginRequest);
}
#endregion
私有void context_BeginRequest(对象发送者,EventArgs e)
{
HttpContext context =(((HttpApplication)sender).Context;
字符串URL = context.Request.Url.ToString();
字符串URLKey = URL.Substring(URL.ToLower().LastIndexOf(&//amp;))+ 1).ToLower();
字符串QueryString =&&";
如果(URLKey.Contains(&?& quot;)))
{
QueryString = URLKey.Substring(URLKey.IndexOf(&?& quot))+ 1);
URLKey = URLKey.Substring(0,URLKey.IndexOf(& quot;?& quot;)));
}
如果(URLKey.Equals(&"test& quot;)))
{
context.RewritePath(& quot; rewrite/rewrite.aspx& quot;);
}
}
}

页面加载事件

公共局部类Rewrite_rewrite:System.Web.UI.Page
{
受保护的void Page_Load(对象发送者,EventArgs e)
{
Response.Write(&测试页加载事件");
}
受保护的void TestRewrite(object sender,EventArgs e)
{
Response.Write(&测试按钮事件&");
}
}

web.config文件设置

< httpModules>
< add name ="UrlRewrite" type ="rewrite,App_Code"/>
</httpModules>

现在,当我调用诸如http://localhost:1404/SampleSite/test之类的URL时,它便可以正常工作并重写到rewrite.aspx页面.但是
当我单击按钮事件时,URL会更改为原始路径,例如http://localhost:1404/SampleSite/rewrite.aspx

在按钮单击事件中,页面只是回发.每当页面回发时,URL都会更改为其原始页面.
我想要的网址是http://localhost:1404/SampleSite/test

谢谢
Imrankhan

Hi friends.
I have a problem to rewrite url using RewritePath method.

Code implementation as below.

Class

public class rewrite : IHttpModule
{
public rewrite()
{
//
// TODO: Add constructor logic here
//
}
#region IHttpModule Members
public void Dispose()
{
//
}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
#endregion
private void context_BeginRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
string URL = context.Request.Url.ToString();
string URLKey = URL.Substring(URL.ToLower().LastIndexOf(&quot;/&quot;) + 1).ToLower();
string QueryString = &quot;&quot;;
if (URLKey.Contains(&quot;?&quot;))
{
QueryString = URLKey.Substring(URLKey.IndexOf(&quot;?&quot;) + 1);
URLKey = URLKey.Substring(0, URLKey.IndexOf(&quot;?&quot;));
}
if (URLKey.Equals(&quot;test&quot;))
{
context.RewritePath(&quot;rewrite/rewrite.aspx&quot;);
}
}
}

Page load event

public partial class Rewrite_rewrite : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(&quot;Test page load event&quot;);
}
protected void TestRewrite(object sender, EventArgs e)
{
Response.Write(&quot;Test button event&quot;);
}
}

web.config file setting

<httpModules>
<add name="UrlRewrite" type="rewrite, App_Code"/>
</httpModules>

Now, when I call URL like http://localhost:1404/SampleSite/test then its working and rewrite to the rewrite.aspx page. but
when I click on button event then URL is changed to original path like as http://localhost:1404/SampleSite/rewrite.aspx

On button click event, page is just post back. whenever page is post back, URL is changed to its original page.
I want URL as it is calling like as http://localhost:1404/SampleSite/test

Thanks
Imrankhan

推荐答案

这是由于表单标签中的操作属性所致.当asp.net呈现html时,它将页面的原始路径置于Form的Action属性中.

解决方案是编写您自己的自定义Form类,如以下文章所述.

http://www.developerfusion.com/article/4643/implementing-http -handlers-in-aspnet/3/ [
It is due to Action Attribute in Form Tag. When asp.net renders html it puts the original Path of the Page in Action attribute of Form.

The solution is to Write you own custom Form class, as described in following Article.

http://www.developerfusion.com/article/4643/implementing-http-handlers-in-aspnet/3/[^]

Nasir Hussain


你好纳西尔·侯赛因. 是的你是对的.我没有考虑过.我打开了您发送的URL,但是该代码有问题.

有以下代码,但是我没有得到Attribute对象名称空间.我正在使用VS-2008.

Hello Nasir Hussain.
Yes you are right. I did not think about it. I opened URL that you sent but I have a problem in that code.

There is following code but I am not getting Attribute object namespace. I am using VS-2008.

protected override void RenderAttributes(HtmlTextWriter writer)
  {
    Attributes.Add("enctype", Enctype);
    Attributes.Add("id", ClientID);
    Attributes.Add("method", Method);
    Attributes.Add("name", Name);
    Attributes.Add("target", Target);
    Attributes.Render(writer);
  }



谢谢



Thanks


这篇关于context.RewritePath在页面回发上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆