简体   繁体   English

使用Global.asax进行URL重写和回发的另一种方法?

[英]An alternative approach to URL Rewriting and Postbacks with Global.asax?

I've been having an issue with URL Rewriting and Postbacks. 我一直遇到网址重写和回发问题。

Edit: Currently using IIS 7 and the URL Rewrite module. 编辑:当前使用IIS 7和URL重写模块。

Basically after a Postback, my URL Rewriting completely messes up and added some duplicated and unnecessary field value pairs to my query string. 基本上在回发之后,我的URL重写完全混乱,并在我的查询字符串中添加了一些重复和不必要的字段值对。 Considering I was parsing the URL in my code, this broke an already working page after a Postback is raised. 考虑到我在我的代码中解析了URL,在引发Postback后,这打破了已经工作的页面。

From what I saw many people before me had the same issue and pretty much all of them have fixed it with modifying the Form Action on PageLoad, like so: 从我看到我之前的许多人都有同样的问题,几乎所有人都通过修改PageLoad上的表单操作修复了它,如下所示:

protected void Page_Load(object sender, EventArgs e)
    {
        form1.Action = Request.RawUrl;

        //Some other interesting things.
    }

Important: This did the trick, it works. 重要提示:这样做很有效 ,但它确实有效。

However, although my developing experience is literally less than a month, I've been trying so far to look for more elegant solutions to my problems. 然而,虽然我的开发经验实际上还不到一个月,但到目前为止,我一直在努力为我的问题寻找更优雅的解决方案。 I've been hinted that there might be a better alternative that involves editing the Global.asax in order to have the same results on a more "global" level. 我一直暗示可能有一个更好的选择,包括编辑Global.asax ,以便在更“全局”的水平上获得相同的结果。

This should, in my opinion, make it more efficient overall as the trick will be done before any other page is called. 在我看来,这应该使整体效率更高,因为在调用任何其他页面之前将完成该技巧。

So my actual question is: 所以我的实际问题是:

How can I achieve the same by editing the Global.asax file instead of modifying the Form Action on my MasterPage loading event? 如何通过编辑Global.asax文件而不是修改我的MasterPage加载事件上的表单操作来实现相同的目的? If you have an even more elegant solution I would appreciate that you include it as well. 如果你有一个更优雅的解决方案,我将不胜感激你也包括它。

Considering this is my first question, I hope I've been constructive enough. 考虑到这是我的第一个问题,我希望我已经足够建设性了。

If you are unable to change the rewrite method then I can think of two approaches which may be better than what you have. 如果你无法改变重写方法,那么我可以想到两种可能比你拥有的方法更好的方法。

1) Create a base page and rewrite the action in it - all pages should then inherit from the new base page. 1)创建一个基页并重写其中的操作 - 然后所有页面都应该从新的基页继承。 This keeps the code in one place and you do not have to write in every page. 这使代码保持在一个位置,您不必在每个页面中写入。

2) Inherit from the default form control and stop it rendering the action property altogether, it will then postback to the rewritten URL instead. 2)从默认表单控件继承并停止它完全呈现action属性,然后它将回发到重写的URL。 Then replace all your form instances with your new control. 然后用新控件替换所有表单实例。 You can see what I mean about half way down this article http://msdn.microsoft.com/library/ms972974 您可以在本文的中间部分看到我的意思http://msdn.microsoft.com/library/ms972974

Edit 编辑

3) Scott Gu posted a solution to this problem (in this article http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx ) which is much cleaner and does not involve changing any code by using Control Adapters. 3)Scott Gu发布了这个问题的解决方案(在这篇文章http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx )这更干净,不涉及使用控制适配器更改任何代码。

There is one more way through iis: 通过iis还有一种方法:

IIS URL Rewriter and explained IIS URL重写器并进行了解释

I advice you to calculate root(in master page property if exists master) like this: 我建议你计算root(在master页面属性中,如果存在master),如下所示:

Root = "http://" + Request.Url.Host + Request.ApplicationPath;
Root += (Root.EndsWith("/") ? "" : "/");

and than paste it in .aspx using this directive: 然后使用此指令将其粘贴到.aspx中:

<%=Root %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM