How do you include .html or .asp file using razor?

2019-01-22 06:39发布

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.

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" -->"

14条回答
Bombasti
2楼-- · 2019-01-22 06:55
@RenderPage("PageHeader.cshtml")
<!-- your page body here -->
@RenderPage("PageFooter.cshtml")

This works just fine and can save you a lot of time.

查看更多
The star\"
3楼-- · 2019-01-22 06:55

Why not include a section within your _Layout.cshtml page that will allow you to render sections based on what menu you want to use.

_Layout.cshtml

<!-- Some stuff. -->
@RenderSection("BannerContent")
<!-- Some other stuff -->

Then, in any page that uses that layout, you will have something like this:

@section BannerContent 
{
  @*Place your ASP.NET and HTML within this section to create/render your menus.*@
}
查看更多
Juvenile、少年°
4楼-- · 2019-01-22 06:55

Using includes is not the correct way to use menus with mvc. You should be using a shared layout and/or partial views.

However if for some odd reason, you must include an html file, here is a way to do it.

Helpers/HtmlHelperExtensions.cs

using System.Web;
using System.Web.Mvc;
using System.Net;

namespace MvcHtmlHelpers
{
    public static class HtmlHelperExtensions
    {
        public static MvcHtmlString WebPage(this HtmlHelper htmlHelper, string serverPath)
        {
            var filePath = HttpContext.Current.Server.MapPath(serverPath);
            return MvcHtmlString.Create(new WebClient().DownloadString(filePath));
        }
    }
}

Add new namespace to web.config

<pages pageBaseType="System.Web.Mvc.WebViewPage">
  <namespaces>
    <add namespace="MvcHtmlHelpers"/>
  </namespaces>
</pages>

Usage:

@Html.WebPage("/Content/pages/home.html")
查看更多
Rolldiameter
5楼-- · 2019-01-22 06:57

Sorry guys for bit old answer but I found some way to attach asp file with razor. Of course you need to do some trick but it works! First of all I created .NET MVC 3 application.

In my _Layout.cshtml I added following line:

@Html.Partial("InsertHelper")

Then I created InsertHelper.aspx in my Shared folder with this content:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

<!--#include VIRTUAL="/ViewPage1.aspx"-->

ViewPage1.aspx is locaited in my root directory, and has just simple if to check whether it works:

<%
string dummy;
dummy="nz";
%>

<% if (dummy == "nz") { %>
nz indeed
<% } else { %>
not nz
<% } %>

And it works!

Razor is able to render partials with different ViewEngine, and that's why this example is working.

And one more thing: remember to not add following line in both aspx files:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

You can add it only once! Hope it helps!

查看更多
淡お忘
6楼-- · 2019-01-22 06:57

you can include server side code and aspx file in .cshtml files as below and then inlude classic asp files or html files. Here are the steps

  1. Index.cshtml
@Html.RenderPartial("InsertASPCodeHelper")

2.InsertASPCodeHelper.aspx

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<!--#include VIRTUAL="~/Views/Shared/Header.aspx"-->
  1. Header.aspx
<!--#include file="/header/header.inc"-->
查看更多
Luminary・发光体
7楼-- · 2019-01-22 07:03

Try making your html page to a cshtml page and including it with:

@RenderPage("_header.cshtml")
查看更多
登录 后发表回答