简体   繁体   English

如何在.Net中使用包含

[英]How to use an Include with .Net

We have used classic ASP at my office for years and are transitioning to ASP.NET. 我们已经在我的办公室使用经典的ASP多年,并且正在过渡到ASP.NET。 The way menus have been done was always by using an "include" statement, and including an html file for the menu (along with some other things). 菜单的完成方式始终是使用“ include”语句,并包括菜单的html文件(以及其他一些东西)。 Obviously with .NET master pages are the answer, but the problem that arises is when we add a new project that wants to basically share a menu with the older classic ASP projects. 显然,.NET母版页是答案,但是出现的问题是,当我们添加一个新项目时,该项目希望与较旧的经典ASP项目基本上共享菜单。 Currently we use Master Pages for the new project and have to update both files/menus when a change is needed. 当前,我们将母版页用于新项目,并且在需要更改时必须更新两个文件/菜单。

I know about Response.WriteFile() but I am not able to get it to work based on the folder structure (as far as I know). 我知道Response.WriteFile(),但是我无法使其基于文件夹结构(据我所知)。 For example, I've tried to use the following on my .NET master page to access the classic menu: 例如,我尝试在.NET主页上使用以下内容访问经典菜单:

<%         
  Response.WriteFile ("../menufolder/menu_body.htm");
%>

With the folder structures looking like, for example, this: 例如,文件夹结构如下所示:

../ClassicASPfolder/menufolder/menu_body.htm  AND
../ClassicASPfolder/apps/ASPNETfolder/masterpage.aspx

This "leaves" the .NET project folder though, and I am guessing that is where the problem lies. 这虽然“离开”了.NET项目文件夹,但我猜这就是问题所在。 Is there something I am missing in regards to the folder structure, or a better solution where we can keep our existing html menus and simply "include" them so that we don't have to maintain two separate menus? 关于文件夹结构,我是否缺少某些东西,或者是更好的解决方案,我们可以保留现有的html菜单并简单地“包括”它们,因此我们不必维护两个单独的菜单?

EDIT: 编辑:

Looking back at my question and considering the XY Problem , is there a solution to "include" htm files as a menu separate from using Response.Write() that works better? 回顾我的问题并考虑XY问题 ,是否有解决方案可以将“ htm文件”作为菜单包含在与使用Response.Write()更好的菜单分开的菜单中? (I am still interested in knowing about the Response.Write folder structure regardless of the solution) (无论解决方案如何,我仍然有兴趣了解Response.Write文件夹结构)

Code for the menu: (the "WebRoot" is currently set from a separate include file, but that can be changed) 菜单代码:(“ WebRoot”当前是通过单独的包含文件设置的,但可以更改)

<div id="MenuContent">
    <div id="Wrapper">
        <div id="Menu">
            <a id="1" href="<%=WebRoot%>/home.asp">Home</a>
            <a id="2" href="<%=WebRoot%>/directory.asp">Directory</a>
            <a id="3" href="<%=WebRoot%>/faq.asp">FAQ</a>
            <a id="4" href="<%=WebRoot%>/information.asp">Information</a>
        </div>
    </div>
</div>

I don't know that much about Classic ASP, but here are my 2 suggestions: 我对Classic ASP不太了解,但是我有2条建议:

  • If you want to keep using your existing menu, you can use a prebuild step to copy the menufile over to the new folder. 如果要继续使用现有菜单,则可以使用预构建步骤将菜单文件复制到新文件夹中。 these are the steps: 这些是步骤:

    1. Open your ASP.NET project; 打开您的ASP.NET项目;
    2. open the properties menu; 打开属性菜单;
    3. go to "Build Events"; 转到“构建事件”;
    4. You have 2 textboxes here, one for Pre-Build events and one for post-Buildevents. 您在这里有2个文本框,一个用于构建前事件,一个用于构建后事件。 These allow you to define specific command lines (like in cmd.exe) to be run before the build starts or after the build has completed. 这些允许您定义特定的命令行(例如cmd.exe中的命令行),以在构建开始之前或构建完成之后运行。 You can use one of these to copy the existing menu_body.htm file to the ASPNETfolder using the copy statement. 您可以使用copy语句使用其中之一将现有的menu_body.htm文件复制到ASPNETfolder。 The syntax is the same as you use when entering a command line statement in cmd.exe. 语法与在cmd.exe中输入命令行语句时使用的语法相同。 I'd start with this syntax: 我将从以下语法开始:

    copy "$(SolutionDir)....\\menufolder\\menu_body.htm" "$(SolutionDir)menu_body.htm" 复制“ $(SolutionDir).... \\ menufolder \\ menu_body.htm”“ $(SolutionDir)menu_body.htm”

    This command (untested) should copy the file to the root folder of your asp.net solution. 此命令(未经测试)应将文件复制到asp.net解决方案的根文件夹。 When you enter it in the pre-build events, it does this before the project is built, if you do it in the postbuild, it will happen after the project is built. 当您在构建前事件中输入它时,它将在构建项目之前执行此操作;如果在后构建中进行,则将在构建项目之后执行。 You only have to edit the one from classic ASP and it will automatically be copied to the new location whenever you build your ASP.NET project. 您只需要从经典ASP中编辑一个代码,并且在您构建ASP.NET项目时,它将自动复制到新位置。

  • A second option, which won't be automatic and is not portable, but which is what I recommend heartily, is to use the ASP.NET menu control. 第二个选项是使用ASP.NET菜单控件,它不是自动的并且不是可移植的,但是我衷心推荐。 the ASP.NET menu control allows you more control over your menu without having to worry about your div locations. ASP.NET菜单控件使您可以更好地控制菜单,而不必担心div的位置。 And because it's default ASP.NET, it fully integrates with your codebehind class. 并且由于它是默认的ASP.NET,因此它与您的codebehind类完全集成。 http://msdn.microsoft.com/en-us/library/ecs0x9w5(VS.80).aspx explains how to use it. http://msdn.microsoft.com/zh-cn/library/ecs0x9w5(VS.80).aspx说明了如何使用它。

    The main issue with this is that it means you cannot reuse your existing .htm file. 这样做的主要问题是,这意味着您无法重用现有的.htm文件。 however, you can use an ASP.NET sitemap file to automatically generate your menu, keeping it up to date with whatever changes you make to your website. 但是,您可以使用ASP.NET站点地图文件自动生成菜单,并使其与您对网站所做的任何更改保持同步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM