如何在MVC中引用全局虚拟包含?

时间:2019-03-07 17:04:57

标签: asp.net-mvc

我的公司有一组全局虚拟文件,它们要用于网站的外观。在ASP时代,您将使用#include virtual =“ etc”。这些是应用程序之外的文件。

在ASP.NET中,您可以使用类似Response.WriteFile ...

但是,如果我尝试在MVC视图中进行操作,则内​​容始终位于页面顶部,而不是位于所需位置。

我将如何在ASP.NET MVC中做到这一点?

1 个答案:

答案 0 :(得分:0)

我实际上使它起作用。我本来想与这些旧的虚拟包含一起工作,但我对公司没有选择。

我是在布局中执行此操作的:我基本上必须将包含带有Response.WriteFile的包含注入到包含所在的目录树中。这些包括用于我们公司的所有网站。然后,我使用StringBuilder在布局中构建内容。我还必须将@RenderBody语句也放入Response对象中。

@using System.Text;
<!DOCTYPE html>
<html>
<head>
   <meta charset="utf-8" />
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <meta name="description" content="Software that excels. Training that inspires.   Build your company mobile presence on Android and Apple."/>
   <meta name="title" content="Software that excels.  Training that inspires.  Build your company mobile presence on Android and Apple." />
   <meta name="robots" content="index, follow" />
<meta name="keywords" content="software, training, Android, mobile, iOS, Apple, data, visualization" />    
<title>@ViewBag.Title</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
<link rel="icon" href="~/Content/favicon3.ico" type="image/x-icon" />
</head>
<body>
    @{
    Response.Buffer = false;
    Response.BufferOutput = false;
    Response.WriteFile("../../includes/header.inc");

    StringBuilder sb = new StringBuilder(4000);
    sb.Append("<div class=\"container body-content\">");
    sb.Append(@RenderBody());
    sb.Append("<hr />");
    sb.Append("<footer>");
    sb.Append("<p>&copy; @DateTime.Now.Year - Excellor Software</p>");
    sb.Append("</footer>" + "</div>");
    sb.Append("</div>");

    Response.Write(sb.ToString());
    sb.Clear();
    sb = null;
    Response.WriteFile("../../includes/footer.inc");
    }
  </body>
  </html>