您如何使用 razor 包含 .html 或 .asp 文件? [英] How do you include .html or .asp file using razor?

查看:21
本文介绍了您如何使用 razor 包含 .html 或 .asp 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 Razor 视图引擎中使用服务器端包含来包含 .html 或 .asp 文件?我们有一个 .html 文件和 .asp 文件,其中包含用于我们所有网站的网站菜单.目前,我们对所有网站都使用服务器端包含,因此我们只需要在一个地方更改菜单.

Is it possible to use server side include in Razor view engine to include .html or .asp file? We have an .html file and .asp files that contain website menus that are used for all of our websitse. Currently we use server side include for all of our sites so that we only need to change the mensu in one place.

我的 _Layout.cshtml 正文中有以下代码

I have the following code in the body of my _Layout.cshtml

<body>
<!--#include virtual="/serverside/menus/MainMenu.asp" -->   
<!--#include virtual="/serverside/menus/library_menu.asp" -->
<!--#include virtual="/portfolios/serverside/menus/portfolio_buttons_head.html" -->
@RenderBody()
</body>

如果我查看源代码,我会看到文字文本,而不是包含文件的内容.

Instead of including the content of the file, if I do a view source, I see the literal text.

" <!--#include virtual="/serverside/menus/MainMenu.asp" --> 
    <!--#include virtual="/serverside/menus/library_menu.asp" -->
    <!--#include virtual="/portfolios/serverside/menus/portfolio_buttons_head.html" -->"

推荐答案

Razor 不支持服务器端包含.最简单的解决方案是将菜单标记复制到您的 _Layout.cshtml 页面中.

Razor does not support server-side includes. The easiest solution would be copying the menu markup into your _Layout.cshtml page.

如果您只需要包含 .html 文件,您可能可以编写一个自定义函数,从磁盘读取文件并写入输出.

If you only needed to include .html files you could probably write a custom function that read the file from disk and wrote the output.

但是,由于您还想包含 .asp 文件(可能包含任意服务器端代码),上述方法将不起作用.您必须有一种方法来执行 .asp 文件,捕获生成的输出,并将其写出到您的 cshtml 文件中的响应中.

However since you also want to include .asp files (that could contain arbitrary server-side code) the above approach won't work. You would have to have a way to execute the .asp file, capture the generated output, and write it out to the response in your cshtml file.

在这种情况下,我会使用复制+粘贴方法

In this case I would go with the copy+paste approach

这篇关于您如何使用 razor 包含 .html 或 .asp 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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