当使用带有 PathInfo 的 URL 时,ASP.NET 回发将不起作用

标签 asp.net asp.net-3.5

我正在用 ASP.NET 3.5† 编写一个网站,现阶段我的大多数 URL 都使用 PathInfo,如下所示:

http://localhost:<whatever>/Account.aspx/Login

唯一的问题是,当表单回发时,它会尝试使用 URL:

http://localhost:<whatever>/Account.aspx/Account.aspx

到目前为止,我已尝试将按钮的 PostbackURL 属性设置为 ~/Account.aspx,但还是发生了同样的情况。

我还尝试过发布到单独的 DoLogin.aspx 页面,但表单变量并未随之发送。

我真的不想在Session集合中设置用户名和密码。

谢谢。

† 我会使用 MVC,但我无法让它与我的主机正常工作。

最佳答案

这些文章提供的解决方法应该可以满足您的需求。

http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx http://blogs.iis.net/ruslany/archive/2008/10/23/asp-net-postbacks-and-url-rewriting.aspx


我相信您的问题与影响任何使用 URL 重写的人的问题相同。尽管方法非常不同,但它们都会导致页面所在位置与系统认为页面所在位置不匹配。

简单问题 - 您是否有机会使用 3.5 SP1?
如果是这样,您也许可以手动设置表单操作 - 基本上类似于

<form ID="ImAForm" action="~/Account.aspx" runat="server> 

不幸的是,我无法确定这是否是同一个问题。从逻辑上讲,这似乎是同一个问题,但我找不到任何特定于 pathInfo 的内容,而且我的大部分经验都是 URL 重写。如果您没有 SP1,则无法轻松覆盖表单操作,因此接下来要尝试的就是第一篇博客中引用的方法...

以下是文章的相关部分...

Handling ASP.NET PostBacks with URL Rewriting

One gotcha that people often run into when using ASP.NET and Url-Rewriting has to-do with handling postback scenarios. Specifically, when you place a control on a page, ASP.NET will automatically by default output the "action" attribute of the markup to point back to the page it is on. The problem when using URL-Rewriting is that the URL that the control renders is not the original URL of the request (for example: /products/books), but rather the re-written one (for example: /products.aspx?category=books). This means that when you do a postback to the server, the URL will not be your nice clean one.

With ASP.NET 1.0 and 1.1, people often resorted to sub-classing the control and created their own control that correctly output the action to use. While this works, it ends up being a little messy - since it means you have to update all of your pages to use this alternate form control, and it can sometimes have problems with the Visual Studio WYSIWYG designer.

The good news is that with ASP.NET 2.0, there is a cleaner trick that you can use to rewrite the "action" attribute on the control. Specifically, you can take advantage of the new ASP.NET 2.0 Control Adapter extensibility architecture to customize the rendering of the control, and override its "action" attribute value with a value you provide. This doesn't require you to change any code in your .aspx pages. Instead, just add a .browser file to your /app_browsers folder that registers a Control Adapter class to use to output the new "action" attribute:

希望有所帮助 - 抱歉,我在最初的帖子中没有说得更清楚。 如果这不起作用,请告诉我...

关于当使用带有 PathInfo 的 URL 时,ASP.NET 回发将不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1271614/

相关文章:

c# - 使用 calendarExtender Ajax 控件时找不到 ExtenderControlBase.cs

javascript - 打开自定义协议(protocol)后关闭浏览器窗口

c# - 如何更改 ASP.NET 中的应用程序语言?

c# - 从文件后面的 asp.net 代码注入(inject) Javascript

asp.net - 我可以在 web.config 中禁用 asp.net web api 路由吗?

ASP.NET MVC FormsAuthentication 检查用户是否登录

javascript - 通过中继器客户端循环

asp.net - 在 TextChanged 上停止回发

security - 编写安全的 ASP.NET 应用程序

.net - WCF 什么时候使用 app.config 或 web.config?