18

I want to use pure html page instead of cshtml with MVC .net. But when I add view by right clicking Index i can see only two options.

 public class HomeController : Controller
{
    //
    // GET: /Home/

    public ActionResult Index()
    {
        return View();
    }}
  1. Cshtml (Razor)
  2. Aspx

I followed Can I serve .html files using Razor as if they were .cshtml files without changing the extension of all my pages?

forum but still no help. I still don’t see an option to add html instead of cshtml

I also tried adding html page directly to view folder but i dont know how to point that view from my controller function.

Replacing Index.cshtml with Index.html gave me this error

The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:

~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Home/Index.cshtml
~/Views/Home/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
Community
  • 1
  • 1
user2132017
  • 245
  • 2
  • 3
  • 7
  • are you trying to serve a brand new html page without it being inside the master layout? – sergioadh Aug 04 '13 at 19:29
  • http://stackoverflow.com/questions/1292475/can-i-stick-a-regular-html-page-in-an-asp-net-mvc-site#answer-1292529 this answer in the POST also works good.... – Savaratkar Mar 28 '14 at 11:15
  • Could somebody fix the code indentation / formatting in this post? – jrh May 21 '17 at 03:09

5 Answers5

14

In order to render plain HTML file you can use

return new FilePathResult(HtmlPath, "text/html");

where HtmlPath is

Server.MapPath(string.Format("~/Views/{0}/{1}.html", YourControllerName, YourHtmlfileName))
abdulbasit
  • 1,856
  • 15
  • 17
  • 4
    Thank you, this is really the correct answer. The accepted answer was *not* pure HTML, and the other answers did not allow proper routing and Controller operation. – Dave Sep 10 '14 at 05:37
  • I want to use .html file only (i don't want that client-side code and server-side code mix together). I tried your suggesting...it's OK, but i have a problem: "how can i use layout"? – Deka May 06 '15 at 04:45
  • @Deka You can't use a layout with pure HTML unless you use a client-side framework to do it. – dwbartz Jul 07 '16 at 14:03
  • @dwbartz: i think so – Deka Jul 09 '16 at 05:19
11

You can create a View with a regular cshtml file add it to the controller and in the View itself just use pure html and add the following to the top:

@{
    Layout = null;
}

This way you use a cshtml file that doesn't use you master layout file. And just serves whatever html you put in it.

sergioadh
  • 1,461
  • 1
  • 16
  • 24
9

If it's a static html file you don't need to point your controller to it, because IIS can serve them just fine. Add an html file to your project (anywhere but NOT in the viewsfolder), and point your browser to that file.

Stephen
  • 2,027
  • 2
  • 22
  • 26
1

If you right click on your Content folder you can select 'Add new item...'. You can then select 'Web' in the tree on the left and choose the 'HTML Page' on the right.

This should add you an HTML page.

Adrian Thompson Phillips
  • 6,893
  • 6
  • 38
  • 69
  • I don’t know how to make my controller point to that html file. Also edited the question to make it more clear – user2132017 Aug 04 '13 at 19:13
  • 2
    Controllers (or rather actions) are 'tied' to views. You would not be serving those pages via controllers, you would serve them via old-fashioned [anchor tags](http://www.w3schools.com/tags/tag_a.asp) – Nameless One Aug 04 '13 at 19:31
  • @user2374966 It might be best to read up on how MVC in ASP.NET works, on one hand there is the whole mechanics of MVC with routes, controllers, view-models and views, on the other hand there is plain-old-content. Your HTML file, like CSS, images, etc. would be plain-old-content. Just a file placed somewhere accessible on the server. – Adrian Thompson Phillips Aug 05 '13 at 08:49
-2

Add a file. Rename it. F2 in solution explorer.

LosManos
  • 7,195
  • 6
  • 56
  • 107