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

后端 未结 14 1524
说谎
说谎 2020-12-14 01:09

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

14条回答
  •  心在旅途
    2020-12-14 01:45

    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

    
      
        
      
    
    

    Usage:

    @Html.WebPage("/Content/pages/home.html")
    

提交回复
热议问题