Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 static files (for some reason, these files must be placed in ~/Content folder). Is there a way that I can tell in my view something like

<% Html.Include("~/Content/snippet.html") %>   // not a real code

?

like image 530
HiveHicks Avatar asked Apr 05 '11 12:04

HiveHicks


People also ask

How can we pass the data from controller to view in MVC?

The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.

How can add Cshtml file in ASP.NET MVC?

Right click the Views\HelloWorld folder and click Add, then click MVC 5 View Page with Layout (Razor). In the Specify Name for Item dialog box, enter Index, and then click OK. In the Select a Layout Page dialog, accept the default _Layout. cshtml and click OK.

How do I serve a static file in NET Core?

To serve static files from an ASP.NET Core app, you must configure static files middleware. With static files middleware configured, an ASP.NET Core app will serve all files located in a certain folder (typically /wwwroot).

How do I access Wwwroot files?

You can access static files with base URL and file name. For example, we can access above app. css file in the css folder by http://localhost:<port>/css/app.css . Remember, you need to include a middleware for serving static files in the Configure method of Startup.


2 Answers

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")) %>
like image 161
Darin Dimitrov Avatar answered Oct 08 '22 16:10

Darin Dimitrov


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")))
like image 20
Olivier de Rivoyre Avatar answered Oct 08 '22 15:10

Olivier de Rivoyre