使用URL重写和404.aspx时的回发问题 [英] Postback problem when using URL Rewrite and 404.aspx
问题描述
我在网站上使用URL重写来获取如下URL:
http://mysite.com/users/john
代替
http://mysite.com/index.aspx?user=john
要使用IIS6实现这种无扩展名的重写,并且不能访问托管服务器,我使用"404方法".当服务器找不到请求时,将执行映射的404页,因为这是一个aspx页,所以可以执行重写(我可以使用托管服务上的控制面板设置404映射). /p>
这是Global.asax中的代码:
protected void Application_BeginRequest(object sender, EventArgs e)
{
string url = HttpContext.Current.Request.Url.AbsolutePath;
if (url.Contains("404.aspx"))
{
string[] urlInfo404 = Request.Url.Query.ToString().Split(';');
if (urlInfo404.Length > 1)
{
string requestURL = urlInfo404[1];
if (requestURL.Contains("/users/"))
{
HttpContext.Current.RewritePath("~/index.aspx?user=" + GetPageID(requestURL));
StoreRequestURL(requestURL);
}
else if (requestURL.Contains("/picture/"))
{
HttpContext.Current.RewritePath("~/showPicture.aspx?pictureID=" + GetPageID(requestURL));
StoreRequestURL(requestURL);
}
}
}
}
private void StoreRequestURL(string url)
{
url = url.Replace("http://", "");
url = url.Substring(url.IndexOf("/"));
HttpContext.Current.Items["VirtualUrl"] = url;
}
private string GetPageID(string requestURL)
{
int idx = requestURL.LastIndexOf("/");
string id = requestURL.Substring(idx + 1);
id = id.Replace(".aspx", ""); //Only needed when testing without the 404-approach
return id;
}
然后,在我的母版页的Page_Load中,我在form-tag的action-attribute中设置了正确的URL.
protected void Page_Load(object sender, EventArgs e)
{
string virtualURL = (string)HttpContext.Current.Items["VirtualUrl"];
if (!String.IsNullOrEmpty(virtualURL))
{
form1.Action = virtualURL;
}
}
重写工作正常,但是当我在页面上执行回发时,未执行回发,可以通过某种方式解决此问题吗?
问题似乎出在404方法上,因为当我尝试不使用它时(并且失去了无扩展功能),回发有效.那是我要求的时间:
http://mysite.com/users/john.aspx
这是否可以解决,或者是否有其他满足我要求的解决方案(IIS6,没有serveraccess/ISAPI过滤器和无扩展名).
form1.Action = Request.RawUrl
结合
HttpContext.Current.RewritePath("/Default.aspx", true);
对我来说很好.
设置表单Action属性是我所缺少的部分...
感谢您的解决方案!
I'm using URL rewrite on my site to get URLs like:
http://mysite.com/users/john
instead of
http://mysite.com/index.aspx?user=john
To achive this extensionless rewrite with IIS6 and no access to the hosting-server I use the "404-approach". When a request that the server can't find, the mapped 404-page is executed, since this is a aspx-page the rewrite can be performed (I can setup the 404-mapping using the controlpanel on the hosting-service).
This is the code in Global.asax:
protected void Application_BeginRequest(object sender, EventArgs e)
{
string url = HttpContext.Current.Request.Url.AbsolutePath;
if (url.Contains("404.aspx"))
{
string[] urlInfo404 = Request.Url.Query.ToString().Split(';');
if (urlInfo404.Length > 1)
{
string requestURL = urlInfo404[1];
if (requestURL.Contains("/users/"))
{
HttpContext.Current.RewritePath("~/index.aspx?user=" + GetPageID(requestURL));
StoreRequestURL(requestURL);
}
else if (requestURL.Contains("/picture/"))
{
HttpContext.Current.RewritePath("~/showPicture.aspx?pictureID=" + GetPageID(requestURL));
StoreRequestURL(requestURL);
}
}
}
}
private void StoreRequestURL(string url)
{
url = url.Replace("http://", "");
url = url.Substring(url.IndexOf("/"));
HttpContext.Current.Items["VirtualUrl"] = url;
}
private string GetPageID(string requestURL)
{
int idx = requestURL.LastIndexOf("/");
string id = requestURL.Substring(idx + 1);
id = id.Replace(".aspx", ""); //Only needed when testing without the 404-approach
return id;
}
And in Page_Load on my masterpage I set the correct URL in the action-attribute on the form-tag.
protected void Page_Load(object sender, EventArgs e)
{
string virtualURL = (string)HttpContext.Current.Items["VirtualUrl"];
if (!String.IsNullOrEmpty(virtualURL))
{
form1.Action = virtualURL;
}
}
The rewrite works fine but when I perform a postback on the page the postback isn't executed, can this be solved somehow?
The problem seems to be with the 404-approach because when I try without it (and loses the extensionless-feature) the postback works. That is when I request:
http://mysite.com/users/john.aspx
Can this be solved or is there any other solution that fulfil my requirements (IIS6, no serveraccess/ISAPI-filter and extensionless).
form1.Action = Request.RawUrl
in combination with
HttpContext.Current.RewritePath("/Default.aspx", true);
works very well for me.
setting the form Action attribute was the piece i was missing...
Thanks for the solution!!!
这篇关于使用URL重写和404.aspx时的回发问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!