ASP.net 不为某些用户代理生成 javascript

发布于 2024-07-23 09:48:10 字数 3113 浏览 5 评论 0原文

******************编辑2************************ 我找到了问题所在...但我不喜欢其中的含义。 我之前正在测试我们的针对 iPhone 的移动应用程序,并使用一个插件将 Firefox 的用户代理字符串伪装成 iPhone。

.Net 实际上并没有仅根据该信息生成回发所需的代码。

然而我不喜欢这样,因为由于 iPhone 和其他多媒体设备可以解释 javascript,ASP.net 正在破坏任何依赖服务器生成的 javascript 运行的应用程序。

所以,如果社区允许的话...我想将我的官方问题更改为...为什么 ASP.net 不会为特定浏览器生成 javascript 以及如何关闭此“功能”。

************** 结束编辑 2 **************

我遇到了一个奇怪的问题。 我将一些工作代码从远程主机复制到我工作的计算机上。 当我尝试使用该页面时,出现 javascript 错误

__doPostBack is not defined
javascript:__doPostBack('ctl00$ContentPlaceHolder1$login','')()()

当我很少输出页面源时,果然没有服务器端生成的 javascript。

我尝试创建一个简单的页面:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="jsTest.aspx.vb" Inherits="_jsTest" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:TextBox ID="tbTest" runat="server"></asp:TextBox><br />
        <asp:LinkButton ID="linkTest" runat="server">LinkButton</asp:LinkButton>
    </form>
</body>
</html>

Codebehind:

Partial Class _jsTest
    Inherits System.Web.UI.Page
    Protected Sub linkTest_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles linkTest.Click
        Response.Write(tbTest.Text)
    End Sub
End Class

遇到相同的错误。

我尝试过重新启动(嘿,一半的时间都有效),清除了 App_Code、global.asax 和 web.config 中的所有内容,添加了一个带有 autopostback=true 的文本框...我没有主意了。

谁能解释一下这里发生的事情吗?

**************更多信息************** 我刚刚在 IE 中再次尝试了所有内容,它按预期工作,页面源显示:

<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTkxNTA2MDE2NWRkxhZMwlMVwJprcVsvQLJLrTcgaSM=" />

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
    theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>
<div>
    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwK20LZAAuzRsusGAsz0+6YPxxO+Ewv1XsD5QKJiiprrGp+9a3Q=" />
</div>

而 Firefox 中的源仅显示:

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTkxNTA2MDE2NWRkxhZMwlMVwJprcVsvQLJLrTcgaSM=" />

<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwK20LZAAuzRsusGAsz0+6YPxxO+Ewv1XsD5QKJiiprrGp+9a3Q=" />

将网页保存到桌面并在记事本中打开显示相同的内容...

********************Edit 2**********************
I figured out the problem... But I don't like the implications. I was testing our iPhone targeted mobile application earlier and using a plugin to mask Firefox's User Agent String as an iPhone.

.Net was infact NOT generating the required code for post backs based on that piece of information alone.

I do not like this however, because since the iPhone and other multimedia devices can interpret javascript, ASP.net is breaking any application that relies on server generated javascript to run.

So, if the community will allow it... I'd like to change my official question to... Why will ASP.net not generate javascript for specific browsers and how can I turn this "feature" off.

*************** End Edit 2 ***************

I've got a weird problem. I copied some working code from my remote host to my computer at work. When I try to use the page I'm getting a javascript error

__doPostBack is not defined
javascript:__doPostBack('ctl00$ContentPlaceHolder1$login','')()()

When I few the output page source, sure enough there is no server side generated javascript.

I tried creating a simple page:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="jsTest.aspx.vb" Inherits="_jsTest" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:TextBox ID="tbTest" runat="server"></asp:TextBox><br />
        <asp:LinkButton ID="linkTest" runat="server">LinkButton</asp:LinkButton>
    </form>
</body>
</html>

Codebehind:

Partial Class _jsTest
    Inherits System.Web.UI.Page
    Protected Sub linkTest_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles linkTest.Click
        Response.Write(tbTest.Text)
    End Sub
End Class

Getting the same error.

I've tried rebooting (hey, it works half the time), cleared out everything from App_Code, global.asax and web.config, added a textbox with autopostback=true... I'm out of ideas.

Can anyone shed some light on what's happening here?

**************More Information**************
I just tried everything again in IE and it works as expected, the page source shows:

<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTkxNTA2MDE2NWRkxhZMwlMVwJprcVsvQLJLrTcgaSM=" />

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
    theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>
<div>
    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwK20LZAAuzRsusGAsz0+6YPxxO+Ewv1XsD5QKJiiprrGp+9a3Q=" />
</div>

While the source in Firefox only shows:

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTkxNTA2MDE2NWRkxhZMwlMVwJprcVsvQLJLrTcgaSM=" />

<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwK20LZAAuzRsusGAsz0+6YPxxO+Ewv1XsD5QKJiiprrGp+9a3Q=" />

Saving the web pages to the desktop and opening in notepad reveals the same thing...

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

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

发布评论

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

评论(6

云朵有点甜 2024-07-30 09:48:10

问题在于 ASP.net 处理未知浏览器的默认方式...例如 iPhone。 尽管假设未知浏览器可以使用 javascript 会很好...您可以在 web.config 或 machine.config 部分中指定浏览器具有哪些功能。

查看 http://sling Five.com/pages/code/browserCaps/ 以获取更新的 browsercaps asp.net 的配置文件

以下是匹配基于 GECKO 的浏览器(Netscape 6+、Mozilla/Firefox,...)的案例示例

<case match="^Mozilla/5\.0 \([^)]*\) (Gecko/[-\d]+)(?'VendorProductToken' (?'type'[^/\d]*)([\d]*)/(?'version'(?'major'\d+)(?'minor'\.\d+)(?'letters'\w*)))?">
                browser=Gecko
                <filter>
                    <case match="(Gecko/[-\d]+)(?'VendorProductToken' (?'type'[^/\d]*)([\d]*)/(?'version'(?'major'\d+)(?'minor'\.\d+)(?'letters'\w*)))">
                        type=${type}
                    </case>
                    <case> <!-- plain Mozilla if no VendorProductToken found -->
                        type=Mozilla
                    </case>
                </filter>
                frames=true
                tables=true
                cookies=true
                javascript=true
                javaapplets=true
                ecmascriptversion=1.5
                w3cdomversion=1.0
                css1=true
                css2=true
                xml=true
                tagwriter=System.Web.UI.HtmlTextWriter
                <case match="rv:(?'version'(?'major'\d+)(?'minor'\.\d+)(?'letters'\w*))">
                    version=${version}
                    majorversion=0${major}
                    minorversion=0${minor}
                    <case match="^b" with="${letters}">
                        beta=true
                    </case>
                </case>
            </case>

The problem is the default way ASP.net treats unknown browsers... such as the iPhone. Even though it would be nice to assume unknown browsers could use javascript... you can specify what capabilities that a browser has in the section of web.config or machine.config.

Check out http://slingfive.com/pages/code/browserCaps/ for an updated browsercaps config file for asp.net

Here is an example of a case to match GECKO Based Browsers (Netscape 6+, Mozilla/Firefox, ...)

<case match="^Mozilla/5\.0 \([^)]*\) (Gecko/[-\d]+)(?'VendorProductToken' (?'type'[^/\d]*)([\d]*)/(?'version'(?'major'\d+)(?'minor'\.\d+)(?'letters'\w*)))?">
                browser=Gecko
                <filter>
                    <case match="(Gecko/[-\d]+)(?'VendorProductToken' (?'type'[^/\d]*)([\d]*)/(?'version'(?'major'\d+)(?'minor'\.\d+)(?'letters'\w*)))">
                        type=${type}
                    </case>
                    <case> <!-- plain Mozilla if no VendorProductToken found -->
                        type=Mozilla
                    </case>
                </filter>
                frames=true
                tables=true
                cookies=true
                javascript=true
                javaapplets=true
                ecmascriptversion=1.5
                w3cdomversion=1.0
                css1=true
                css2=true
                xml=true
                tagwriter=System.Web.UI.HtmlTextWriter
                <case match="rv:(?'version'(?'major'\d+)(?'minor'\.\d+)(?'letters'\w*))">
                    version=${version}
                    majorversion=0${major}
                    minorversion=0${minor}
                    <case match="^b" with="${letters}">
                        beta=true
                    </case>
                </case>
            </case>
夏花。依旧 2024-07-30 09:48:10

在重新安装 Firefox 之前,请以调试模式运行它(我认为这称为调试模式)。 它会关闭所有插件,这可以帮助您缩小范围。 Chrome 或 Safari 等其他浏览器怎么样?

Before you reinstall Firefox, run it in debug mode (I think it's called debug mode). It turns off all plugins and that can help you narrow it down a bit. What about other browsers like Chrome or Safari?

柠檬色的秋千 2024-07-30 09:48:10

根据新的信息,我认为很明显这是一个 Firefox 问题(也许你有一个阻止 JS 的附加组件),而不是一个编程问题。 我在 XP Pro 上使用 VS 2008 和 FF3 使用你的代码得到了很好的结果,正如我期望大多数其他人尝试它一样。

您可以尝试重新安装 Firefox,确保 JS 在所有其他站点上正常工作,确保 localhost 没有不同的安全权限...

Based on the new information, I think it's clear that this is a Firefox problem (perhaps you have an add-on blocking JS), and not a programming question. I get fine results with your code using VS 2008 and FF3 on XP Pro, as I'd expect will most anyone else trying it.

You may try reinstalling Firefox, ensure that JS works on all other sites, make sure localhost doesn't have different security permissions...

小耗子 2024-07-30 09:48:10

您已将 AutoEventWireup 设置为 false,但没有重写 OnInit 来附加事件。 尝试将 AutoEventWireup 更改为 true。

编辑:从更多信息来看,可能是它在 machine.config 的浏览器功能部分错误地识别了 Firefox。 (或 web.config)。

也可能是 JavaScript 在 Firefox 中被关闭,因此 .NET 确定渲染 Javascript 内容没有意义,并且应该使用不同的方法来进行回发处理(如果存在这种情况)。

You have AutoEventWireup set to false, but no Override of OnInit to attach the event. Try changing the AutoEventWireup to true.

Edit: From the more information it could be that it is incorrectly identifying Firefox in the brower capabilities section of your machine.config. (or web.config).

It could also be that JavaScript is turned off in Firefox, and thus .NET is determining that there is no point rendering the Javascript stuff, and should use a different approach to postback handling, if there is such a thing.

一刻暧昧 2024-07-30 09:48:10

您确定您的 Web 服务器上安装了 ASP.NET 吗?

Are you sure you have ASP.NET installed on your web server?

撞了怀 2024-07-30 09:48:10

看起来 __doPostBack() 函数没有生成,因为您没有需要它的服务器端事件。

ASP.NET 仅在您订阅了需要该函数正常运行的事件侦听器时才生成 __doPostBack() 函数。

It looks like the __doPostBack() function is not being generated because you have no server-side events which require it.

ASP.NET likes to only generate the __doPostBack() function when you have event listeners subscribed that would need it to function correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文