ASP.net 不会填充 action=""发布时的服务器表单

发布于 2025-01-02 00:53:15 字数 1115 浏览 0 评论 0 原文

我有一个 ASP.net Web 表单站点,母版页中有一个服务器表单,因为所有页面都需要它。

调试时,action 参数在运行时与 id 一起填充,但是当使用 IIS7 部署在我的服务器上时,它不会出现......但仍然有效。它不会导致网站问题,但会导致我的 W3C HTML5 验证失败,因为需要填充它。

调试源:

<form method="post" action="index.aspx" id="aspnetForm">

实时源:母

<form method="post" action="" id="aspnetForm">

版页中的表单声明:

<form runat="server">
.. some divs
</form>

我知道表单标记没有定义 ID/操作等,因为 ASP 在运行时配置默认值,这很好,尽管由于某种原因它在我的服务器上弄乱了。我尝试使用 action="<% Path etc %>" 来获取路径名称,但它不起作用。

我做错了什么?我是否遗漏了某些内容,或者在母版页中使用表单只是一种不好的做法?

谢谢。

更新

好的,为了解决答案中指出的问题,我只是在Masterpage Page_Load上设置了Form.Action,终于得到了W3C的绿灯!

注意:我使用的是 Intelligencia Rewriter,但您可以使用 Request.Url 提取 URL

public partial class myMasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Form.Action = Intelligencia.UrlRewriter.RewriterHttpModule.RawUrl;
    }
}

I have an ASP.net webforms site, with a server form in the masterpage, as all pages require it.

When debugging the action parameter is populated at runtime along with the id, but when deployed on my server with IIS7 it doesn't appear... but still works. It's not causing the site issues, but its making my W3C HTML5 validation fail, as it needs to be populated.

Debug source:

<form method="post" action="index.aspx" id="aspnetForm">

Live source:

<form method="post" action="" id="aspnetForm">

Form declaration in masterpage:

<form runat="server">
.. some divs
</form>

I know the form tag doesnt have ID/action defined etc, because ASP configures the default at runtime and thats fine, although for some reason it messes up on my server. I've tried using the action="<% Path etc %>", to get the path name but it doesn't work.

What am I doing wrong? Am I missing something, or is it just bad practise to use a form in a masterpage?

Thanks.

Update

Ok, to solve the issue pointed out in the answer I just set the Form.Action on the Masterpage Page_Load, finally got that W3C green light!

Note: I'm using Intelligencia Rewriter, but you can pull the URL using Request.Url

public partial class myMasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Form.Action = Intelligencia.UrlRewriter.RewriterHttpModule.RawUrl;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南笙 2025-01-09 00:53:15

ASP.NET 4.0?如果是这样,这就是您的答案: http://www.asp.net/whitepapers /aspnet4/break-changes#0.1__Toc256770154;这不是一本容易阅读的书,而且我自己也不熟悉,所以您可能应该阅读它并了解它如何影响您的网站。

asp.net 4.0? If so, here's your answer: http://www.asp.net/whitepapers/aspnet4/breaking-changes#0.1__Toc256770154; It's not an easy read, and I'm not familiar myself, so you should probably read it and see how that affects your site.

安静 2025-01-09 00:53:15

我刚刚看到你的帖子,有完全相同的经历。在我看来,这是不直观的设计行为,会给大多数试图掌握兼容 HTML5 的 ASP.NET 新手开发人员带来困惑。

无论如何,经过一些篡改后,可以使用以下方法轻松解决问题。 无需篡改 web.config - 谢天谢地!

在母版页中,添加此代码段(或者在出现此问题的默认页面中,如果您不使用母版页)

Protected Sub Page_PreRender(sender As Object, e As EventArgs) Handles Me.PreRender
    If Not Page.IsPostBack Then
        If Page.Form.Action = String.Empty Then
            Page.Form.Action = Request.Url.AbsoluteUri
        End If
    End If
End Sub

I just saw your post having had exactly the same experience. In my opinion this is unintuitive by-design behaviour that will bring confusion to the majority of newer ASP.NET developers trying to grasp compliant HTML5.

Anyway after some tampering the problem can be fixed very easily using the following approach. There is no need to tamper with the web.config - thankfully!

In the Master page, add this snippet (or in the default page where this problem arises, if you're not using a master page)

Protected Sub Page_PreRender(sender As Object, e As EventArgs) Handles Me.PreRender
    If Not Page.IsPostBack Then
        If Page.Form.Action = String.Empty Then
            Page.Form.Action = Request.Url.AbsoluteUri
        End If
    End If
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文