使用#include还是有正确的.net方法? [英] Use #include or is there a proper .net way?

查看:110
本文介绍了使用#include还是有正确的.net方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在更新我公司的网站.在过去的八年左右的时间里,不同的程序员在将通用文件包含到页面中的方式上一直存在不一致的地方:有些使用了旧的<!-- #include ...指令,有些使用了<% Response.WriteFile(Request.MapPath(...以及一些变体.

这些方法之间有什么有效的区别吗?

我还想编写一个工具箱函数,该函数将打开文件并以字符串形式返回文本,因此我可以执行类似<% Toolbox.Include(filename) %>的操作.我喜欢这个想法,因为它为其他程序员提供了一致的工具.

I am currently updating my company''s website. Over the last 8 years or so, different programmers have been inconsistent in how they include common files into pages: some used the old <!-- #include ... directive, others used <% Response.WriteFile(Request.MapPath(..., along with several variations.

Is there any effective difference between these methods?

I would also like to write a toolbox function that would open the file and return the text as a string, so I could do something like <% Toolbox.Include(filename) %>. I like this idea as it gives other programmers a consistent tool. Would this likely decrease performance?

推荐答案

我认为您在想的更像是老式ASP方式,而不是Asp.net方式.
当然,您可以开发一个接受文件名(Aspx),执行该文件并返回响应的工具.以下代码描述了这个想法:

I think you are thinking more like old style ASP way, rather than the Asp.net way.

Of course, you can develop a tool that accepts a file name (Aspx), executes it and returns the response. The following code depicts the idea:

StringWriter writer = new StringWriter();
Server.Execute("Login.aspx", writer);
Response.Write("<H3>Please Login:</H3><br>"+ writer.ToString());



但是,Asp.net中有一种更优雅的方式来重用页面或模块.与老式的Asp #include相比,您可以使用User Control和Master页面概念以更易于管理的方式重用公共页面/模块.

在Asp.net中,您可以开发一些用户控件,其中包含可在不同页面上使用的可重复使用的GUI和功能,并且只需将这些用户控件包括在所需的任何页面中即可.请参阅 http://msdn.microsoft.com/en-us/library/fb3w5b53 %28VS.71%29.aspx [ ^ ]了解如何开发和使用用户控件.

并且,您还可以开发母版页,并在母版页中包含常用的用户控件.不必在每个页面上单独添加用户控件,而是通过将页面连接到母版页,就可以在整个页面上重用通用的GUI和功能.参见 http://www.asp.net/master-pages/tutorials [



But, there is more elegant way in Asp.net to reuse the page or modules. There is User Control and Master page concept which you can use to re-use your common page/modules in a more manageable way than the old fashioned Asp #include.

In Asp.net, you can develop some user controls that would contain the re-usable GUI and functionality that can be used across different pages and you just include these user controls in whatever pages you require. See http://msdn.microsoft.com/en-us/library/fb3w5b53%28VS.71%29.aspx[^] to learn how to develop and use user controls.

And, you can also develop Master page(s) and include the common user controls within the Master page(s). Instead of adding the user controls individually at each page, this will allow you to reuse the common GUI and functionality across the pages by just hooking up your pages to the Master page(s). See http://www.asp.net/master-pages/tutorials[^] to learn how to develop and use Master pages.

As you seem more used to traditional Asp based programming model, you might need some time to cope up with the Asp.net''s User control and Master page concept and utilize the full power of them. But once you become familiar with these, I bet you will love those.

Asp.net will let you work "smarter", not "harder". You will love it.


我想出了我想要的答案.

<-- #include file="..." -->在页面循环开始之前执行,而Response.WriteRender期间执行.因为某些包含文件具有自己的内联ASP代码,所以必须使用#include指令将它们引入,以便在需要时可以使用该代码.

无论如何,谢谢.
I figured out the answer I need.

<-- #include file="..." --> is executed before the page cycle begins, while Response.Write is executed during Render. Because some of the include files have their own in-line ASP code, they must be brought in with the #include directive so that the code is there when needed.

Thanks anyway.


当您想使用"include file =的方式包含文本时,可能会有一些有效的方案.但是,大多数时候您会想要使用其他方式.例如,如果要包括一些设置,通常将其存储在Web.config中并从后面的代码中加载它.对于基于用户的设置,通常会将其存储在数据库/会话/cookie/查询字符串/表单状态中.而且,如果您想重用代码,可以将其放在一个类中,然后从后面的代码中使用它.而且,如果您想重用标记,可以将其放在用户控件中,该控件可以选择接受各种参数,以根据标记的使用位置进行不同的呈现.在ASP.Net中,这些通常是正确的.Net方式".
There may be some valid scenarios for when you would want to use the "include file=" way of including text. However, most of the time you''ll want to use alternative ways. For example, if you want to include some settings, you would usually store that in the Web.config and load it up from the code behind. For user-based settings, you''d typically store that in a database/session/cookie/query string/form state. And if you want to reuse code, you''d put it in a class and use that from the code behind. And if you want to reuse markup, you''d put that in a user control that, optionally, accepts various parameters to render differently based on where it is used. These are typically the "proper .Net ways" of doing things in ASP.Net.


这篇关于使用#include还是有正确的.net方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆