Skip to content Skip to sidebar Skip to footer

Including Static Html File From ~/content Into Asp.net Mvc View

I've got master page in my project, which contains some information about site copyright and some contact info in it. I'd like to take it out of master page and place it in a stati

Solution 1:

You are better off using a partial view (even if it only contains static text) and include it with the Html.Partial helper. But if you insist:

<%= File.ReadAllText(Server.MapPath("~/Content/snippet.html")) %>

Solution 2:

If you are in .Net MVC 5 and want to include a HTML file a partial file without having Razor that render it:

@Html.Raw(File.ReadAllText(Server.MapPath("~/Views/Shared/ICanHaz.html")))

Solution 3:

Use your own view engine

using

@Html.Partial("_CommonHtmlHead")

as

/Views/Shared/_CommonHtmlHead.html

or

/Views/MyArea/Shared/_CommonHtmlHead.htm

is the best for me.

Creating your own view engine for this by using the System.Web.Mvc.VirtualPathProviderViewEngine ascending class seems to be relatively easy:

///<summary>/// Simple render engine to load static HTML files, supposing that view files has the html/htm extension, supporting replacing tilda paths (~/MyRelativePath) in the content///</summary>publicclassHtmlStaticViewEngine : VirtualPathProviderViewEngine
{
    privatestaticreadonly ILog _log = LogManager.GetLogger(typeof (HtmlStaticViewEngine));

    protectedreadonly DateTime? AbsoluteTimeout;
    protectedreadonly TimeSpan? SlidingTimeout;
    protectedreadonly CacheItemPriority? Priority;

    privatereadonlybool _useCache;

    publicHtmlStaticViewEngine(TimeSpan? slidingTimeout = null, DateTime? absoluteTimeout = null, CacheItemPriority? priority = null)
    {
        _useCache = absoluteTimeout.HasValue || slidingTimeout.HasValue || priority.HasValue;

        SlidingTimeout = slidingTimeout;
        AbsoluteTimeout = absoluteTimeout;
        Priority = priority;

        AreaViewLocationFormats = new[]
        {
            "~/Areas/{2}/Views/{1}/{0}.html",
            "~/Areas/{2}/Views/{1}/{0}.htm",
            "~/Areas/{2}/Views/Shared/{0}.html",
            "~/Areas/{2}/Views/Shared/{0}.htm"
        };
        AreaMasterLocationFormats = new[]
        {
            "~/Areas/{2}/Views/{1}/{0}.html",
            "~/Areas/{2}/Views/{1}/{0}.htm",
            "~/Areas/{2}/Views/Shared/{0}.html",
            "~/Areas/{2}/Views/Shared/{0}.htm"
        };
        AreaPartialViewLocationFormats = new[]
        {
            "~/Areas/{2}/Views/{1}/{0}.html",
            "~/Areas/{2}/Views/{1}/{0}.htm",
            "~/Areas/{2}/Views/Shared/{0}.html",
            "~/Areas/{2}/Views/Shared/{0}.htm"
        };

        ViewLocationFormats = new[]
        {
            "~/Views/{1}/{0}.html",
            "~/Views/{1}/{0}.htm",
            "~/Views/Shared/{0}.html",
            "~/Views/Shared/{0}.htm"
        };
        MasterLocationFormats = new[]
        {
            "~/Views/{1}/{0}.html",
            "~/Views/{1}/{0}.htm",
            "~/Views/Shared/{0}.html",
            "~/Views/Shared/{0}.htm"
        };
        PartialViewLocationFormats = new[]
        {
            "~/Views/{1}/{0}.html",
            "~/Views/{1}/{0}.htm",
            "~/Views/Shared/{0}.html",
            "~/Views/Shared/{0}.htm"
        };

        FileExtensions = new[]
        {
            "html",
            "htm",
        };
    }

    protectedvirtualstringGetContent(string viewFilePath)
    {
        string result = null;
        if (!string.IsNullOrWhiteSpace(viewFilePath))
        {
            if (_useCache)
            {
                result = TryCache(viewFilePath);
            }

            if (result == null)
            {
                using (StreamReader streamReader = File.OpenText(viewFilePath))
                {
                    result = streamReader.ReadToEnd();
                }

                result = ParseContent(result);

                if (_useCache)
                {
                    CacheIt(viewFilePath, result);
                }
            }
        }

        return result;
    }

    staticreadonly Regex TildaRegularExpression = new Regex(@"~/", RegexOptions.Compiled);

    ///<summary>/// Finds all tilda paths in the content and replace it for current path///</summary>///<param name="content"></param>///<returns></returns>protectedvirtualstringParseContent(string content)
    {
        if (String.IsNullOrWhiteSpace(content))
        {
            return content;
        }

        string absolutePath = VirtualPathUtility.ToAbsolute("~/");

        string result = TildaRegularExpression.Replace(content, absolutePath);
        return result;
    }

    protectedoverride IView CreatePartialView(ControllerContext controllerContext, string partialPath)
    {
        HttpContextBase httpContextBase = controllerContext.RequestContext.HttpContext;

        string filePath = httpContextBase.Server.MapPath(partialPath);
        string content = GetContent(filePath);
        returnnew StaticView(content);
    }

    protectedoverride IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
    {
        HttpContextBase httpContextBase = controllerContext.RequestContext.HttpContext;
        
        string result = null;
        if (!string.IsNullOrWhiteSpace(masterPath))
        {
            string filePath = httpContextBase.Server.MapPath(masterPath);
            result = GetContent(filePath);
        }

        string physicalViewPath = httpContextBase.Server.MapPath(viewPath);
        result += GetContent(physicalViewPath);
        returnnew StaticView(result);
    }

    protectedvirtualstringTryCache(string filePath)
    {
        HttpContext httpContext = HttpContext.Current;
        if (httpContext != null && httpContext.Cache != null)
        {
            string cacheKey = CacheKey(filePath);
            return (string)httpContext.Cache[cacheKey];
        }
        returnnull;
    }

    protectedvirtualboolCacheIt(string filePath, string content)
    {
        HttpContext httpContext = HttpContext.Current;
        if (httpContext != null && httpContext.Cache != null)
        {
            string cacheKey = CacheKey(filePath);
            httpContext.Cache.Add(cacheKey, content, new CacheDependency(filePath), AbsoluteTimeout.GetValueOrDefault(Cache.NoAbsoluteExpiration), SlidingTimeout.GetValueOrDefault(Cache.NoSlidingExpiration), Priority.GetValueOrDefault(CacheItemPriority.AboveNormal), CacheItemRemovedCallback);
            returntrue;
        }

        returnfalse;
    }

    protectedvirtualstringCacheKey(string serverPath)
    {
        return serverPath;
    }

    protectedvirtualvoidCacheItemRemovedCallback(string key, objectvalue, CacheItemRemovedReason reason)
    {
        _log.InfoFormat("CacheItemRemovedCallback(string key='{0}', object value = ..., {1} reason={2})", key, reason.GetType().Name, reason);
    }
}

publicclassMvcApplication : System.Web.HttpApplication
{
    protectedvoidApplication_Start()
    {
        ViewEngines.Engines.Add(new HtmlStaticViewEngine(new TimeSpan(12,0,0,0)));
    }
}

publicclassStaticView : IView
{
    privatereadonlystring _text;

    publicStaticView(string text)
    {
        _text = text;
    }

    publicvoidRender(ViewContext viewContext, TextWriter writer)
    {
        if (! string.IsNullOrEmpty(_text))
        {
            writer.Write(_text);
        }
    }
}

NOTE: This code is tested only with simple usage for rendering partial views

Solution 4:

Is there a reason you are holding the content in an HTML file rather than a partial view?

If you create a file called snippet.ascx in your Views/Shared folder you can import the content of that snippet.ascx file into any view by using <% Html.RenderPartial("snippet"); %>

Solution 5:

To include static html file into a MVC View goes like this:

<!-- #include virtual="~\Content\snippet.htm" -->

Post a Comment for "Including Static Html File From ~/content Into Asp.net Mvc View"