ASP.NET:代码隐藏还是无代码隐藏?
为什么有人不想使用代码隐藏文件,以便将服务器端代码与标记分开?这难道不是 .NET 相对于传统 ASP 的优势之一吗?
就我个人而言,我认为将代码与标记混合会使代码更难理解。
我讨厌看到那些该死的<%%> (服务器端块)与标记相互拼接,恶心。我希望这在 ASP.NET 中只是为了向后兼容经典 ASP,但我一直看到 MS 的示例包含那些黄色括号。
我试图理解可在此处下载的代码示例,并且感到困惑为什么此处显示的任何服务器端中断在执行代码时都不会中断,即使我查看 web.config 中已设置。由于我通常使用代码隐藏,因此我想知道 aspx 中是否存在以不同方式处理的服务器端代码,从而阻止我调试 runat=server 代码。
所以。我的问题是:
1)为什么有人不想使用代码隐藏文件,以便将服务器端代码与标记分开?
2)为什么我无法打破服务器端逻辑?
也欢迎您对我的任何相关评论发表见解和意见。
Why would anyone want to not use a code behind file so that server sided code is separated from markup? Wasn't that supposed to be one of the advantages of .NET over classic ASP?
Personally, I think mixing code with markup makes the code a lot harder to understand.
I hate to see those darn <% %> (server sided blocks) inter-spliced in with markup, yuck. I would expect that this is in ASP.NET solely for backward compatibility with Classic ASP but I see examples from MS all the time that include those yellow brackets.
I am trying to understand a code example which is available for download here and puzzled at why any of my server-sided breaks shown here don't break when executing the code even though I see that has been set in the web.config. Since I usually work with code-behinds, I am wondering if there is something about server-side code in the aspx that is handled differently that is preventing me from debugging the runat=server code.
So. My questions are:
1) Why would anyone want to not use a code behind file so that server sided code is separated from markup?
2) Why might I not be able to break on the server sided logic?
Your insight and opinions are also welcomed on any of my related comments.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用
<% %>
的内联代码能力不仅仅是为了向后兼容,而且是 .NET 的一个功能,可以允许一些(相对!)清晰和直接的解决方案。然而,它经常以不太理想的方式使用。同样,代码隐藏中的代码经常(实际上通常)以不太理想的方式使用,就像 Web 控件一样。将代码放在代码隐藏中通常不会用于单独的关注点,而是会产生混乱的意大利面条式代码与我们在经典 asp 中的位置不同。 .NET 确实允许您拥有组织良好的解决方案,但实现这一目标取决于您。在代码隐藏页面中添加代码不是该旅程的第一步,只是该旅程可以开始的地方。
至于为什么您的事件没有触发,最有可能的是:
The inline code ability using
<% %>
is not just for backward compatibility, but a feature of .NET that can allow for some (relatively!) clear and straightforward solutions. It is however often used in a less than ideal way. Likewise, code in the code-behind if often (usually in fact) used in a less than ideal way, as are web-controls.Having code in the code-behind typically does not serve to separate concerns, but to have jumbled spaghetti code in a different place than we had it in classic asp. .NET does allow you to have very well organized solutions, but it's up to you to make it happen. Having code in code behind pages is not the first step in that journey, just where that journey can begin.
As to why your events are not firing, most likely:
1) 我想如果您习惯于开发经典的 ASP,那么这是一个简单的过渡。
2)如果没有看到您的标记,我将无法告诉您您的问题是什么。如果您没有到达该断点,可能有以下几个原因之一:
1) I guess if you're used to developing classic ASP, then its an easy transition.
2) Without seeing your markup, I wouldn't be able to tell you what your issue is. If you're not hitting that breakpoint, there could be one of a few reasons:
大多数 MS 示例中使用的括号通常用于函数调用或引用数据绑定器。
例如,
<%# Databinder.Eval("MyColumn") %>
将在转发器中使用。还有一些标记用于引用 web.config 属性,例如连接字符串
<%$ConnectionStrings:NorthwindConnection %>
The brackets used in most of the MS examples are usually for function calls or referencing a databinder.
For example,
<%# Databinder.Eval("MyColumn") %>
would be used in a repeater.There are also tags that are used to reference web.config attributes like the connection string
<%$ConnectionStrings:NorthwindConnection %>
“示例”ASP.NET 代码经常与内联代码一起分发,只是因为这样分发更容易。它是独立的,您只需将其复制并粘贴到记事本中,将其另存为测试站点文件夹中的 .aspx,然后查看它如何运行。这可能不是您想要在生产中做的事情。
至于更一般的问题... ASP.NET MVC 在技术上仍然是 ASP.NET,并且不使用代码隐藏文件。许多开发人员认为代码隐藏文件只是将一种丑陋换成了另一种丑陋。就我个人而言,我可以从两方面看到这一点,但我认为经典 ASP 中出现如此多糟糕代码的真正原因不是标签汤,而是人们在“视图”代码中做了疯狂的事情,例如打开数据库连接。只要你不做那种事,几个服务器标签根本不是什么大不了的事。
事实上,如果您进行任何数据绑定,您将会有一堆
Eval
和Bind
标记与标记混合在一起。因此,即使是带有代码隐藏的纯 WebForms 也并不总是一尘不染。Very often "sample" ASP.NET code is distributed with inline code simply because, well, it's easier to distribute that way. It's self-contained, you can just copy and paste it into notepad, save it as an .aspx in the folder for a test site, and see how it runs. It's probably not something you want to do in production.
As for the more general question... ASP.NET MVC is technically still ASP.NET and does not use code-behind files. Many developers feel that code-behind files just traded one type of ugliness for another; personally, I can see it from both sides, but I think the real reason for so much lousy code in classic ASP wasn't the tag soup, but the fact that people were doing insane things in "view" code like opening up database connections. As long as you don't do that kind of thing, a few server tags are not a big deal at all.
In fact, if you do any data binding you're going to have a bunch of
Eval
andBind
tags mixed in with the markup. So even pure WebForms with code-behind isn't always squeaky-clean.当您开始调试时,构建可能失败,并且您没有注意到选择运行以前的构建。您的断点可能不存在于该构建中。
It's possible that the build failed when you started debugging, and you didn't notice electing to run the previous build. Your breakpoints may not have existed in that build.
<%# DataBinder.Eval("Column") %>
可以从后面的代码中节省大量额外的代码,在我看来,这种做法并不是那么糟糕。<%# DataBinder.Eval("Column") %>
can save you a lot of extra code from the code behind, and it's not that bad of a practice in my opinion.