是否可以修改 ASP.NET 以不再需要 runat=“server”?

发布于 2024-08-31 14:26:20 字数 538 浏览 2 评论 0原文

我知道为什么当前需要 runat="server" (ASP.NET 为什么 runat="server"),但共识是,如果您将简单的默认值纳入设计中,则不需要它(我当然同意)。

是否可以修改、扩展、反编译和重新创建、拦截或以其他方式更改 ASP.NET 解析 ASPX 和 ASCX 文件的行为,以便不再需要 runat="server"?例如,我假设可以对 Mono 的一个版本进行分支来实现此目标。

如果特定要求有帮助,下面重点介绍一种设计:

  • 在解析期间,当遇到配置的命名空间标签(例如“asp”)时,将元素的 runat 属性默认为“server”
  • 在解析期间,当遇到配置的命名空间标签(例如如“asp”),如果元素的 runat 属性值可用,则应使用该值代替默认
  • 引入的新页面级设置(可以在页面指令或 web.config 中设置),该设置指定默认 runat特定命名空间标签的值

I know why runat="server" is currently required (ASP.NET why runat="server"), but the consensus is that it should not be required if you incorporate a simple default into the design (I agree of course).

Would it be possible to modify, extend, decompile and recreate, intercept or otherwise change the behavior of how ASP.NET parses ASPX and ASCX files so that runat="server" would no longer be required? For instance, I assume that a version of Mono could be branched to accomplish this goal.

In case specific requirements are helpful, the following highlights one design:

  • During parsing, when configured namespace tags are encountered (such as "asp"), default the element's runat property to "server"
  • During parsing, when configured namespace tags are encountered (such as "asp"), if the element's runat property value is available, then that value should be used in place of the default
  • New page-level setting introduced (can be set in the page directive or web.config) that specifies the default runat value for a specific namespace tag

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

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

发布评论

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

评论(2

梦行七里 2024-09-07 14:26:20

恐怕您必须修改整个页面解析器才能完成此任务,我认为这是不可能的。

另一方面,您应该能够创建自己的。请参阅 buildProviders 元素BuildProvider 类。您应该能够为 .aspx 页面创建自己的构建提供程序并使用它来替换内置提供程序。

不幸的是,ASP.NET 使用的 PageBuildProvider 类是内部的,而它用来解析页面的 PageParser 类是sealed。你将完全依靠自己。

考虑到 runat="server" 已经在 ASP.NET 中存在了十年,我想您会发现这种情况不会很快改变。

您还会失去设计师的支持,但也许您并不关心这一点。

I'm afraid you'd have to modify the entire page parser to accomplish this, and I don't think that's possible.

On the other hand, you should be able to create your own. See the buildProviders Element and the BuildProvider class. You should be able to create your own build provider for .aspx pages and use it to replace the built-in provider.

Unfortunately, the PageBuildProvider class used by ASP.NET is internal, and the PageParser class that it uses to parse pages is sealed. You'll be entirely on your own.

Consider that runat="server" has been in ASP.NET for a decade now, and I think you'll see that this won't change anytime soon.

You'd also lose Designer support, but maybe you don't care about that.

三五鸿雁 2024-09-07 14:26:20

据我所知,ASP.NET 页面处理过程中没有足够深的钩子允许这样做。我知道没有办法覆盖或扩展实际 aspx/ascx 代码的解析或处理。

虽然 ASP.NET 相当灵活,允许您覆盖许多默认行为(例如 ViewState 的保存/加载方式、Session 的存储位置等),但这不是其中之一。

然而......从技术上讲,Page 对象只是另一个 HttpHandler。您可以编写您的处理程序并用它做任何您想做的事情。您所要做的就是实现 Page 类所做的所有事情,然后添加这个额外的功能。 :) 或者,拉出 Reflector 并深入研究 Page 对象的 ProcessRequest 方法,看看它实际上在哪里解析/初始化 aspx 中声明的对象,您可能会得到如何实现您正在寻找的功能的线索。但我怀疑你会浪费时间。

So far as I know, there are no hooks deep enough in the ASP.NET Page handling process that would allow this. I know of no way to override or extend the parsing or processing of actual aspx/ascx code.

While ASP.NET is fairly flexible and lets your override many default behaviors (like how ViewState is saved/loaded, where Session is stored, etc) this is not one of them.

However... technically the Page object is just another HttpHandler. You could write your handler and do anything you wanted with it. All you have to do is implement everything the Page class does and then throw in this extra functionality. :) Alternately, pull out Reflector and dig through the Page object's ProcessRequest method and see where it is actually parsing/initializing the objects declared in aspx and you might get a clue how to implement the functionality you're looking for. But I suspect you'd be wasting your time.

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