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
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"-->
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.*@
}
Try making your html page to a cshtml page and including it with:
@RenderPage("_header.cshtml")
Razor does not support server-side includes. The easiest solution would be copying the menu markup into your _Layout.cshtml page.
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.
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
I had the same issue when I tried to include an .inc
file in MVC 4.
To solved this issue, I changed the suffix of the file to .cshtml
and I added the following line
@RenderPage("../../Includes/global-banner_v4.cshtml")
@RenderPage("PageHeader.cshtml")
<!-- your page body here -->
@RenderPage("PageFooter.cshtml")
This works just fine and can save you a lot of time.