Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Html instead of csHtml

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
like image 793
user2132017 Avatar asked Aug 04 '13 18:08

user2132017


People also ask

Is Cshtml same as HTML?

Cshtml is basically razor view extension and any view renders in html finally. You need to use Razor in your application as it supports server side code but raw html does not.

Is Cshtml the same as Razor?

cshtml is just a file extension. razor view engine is used to convert razor pages(. cshtml) to html.

What is the difference between ASPX and Cshtml?

One major advantage to aspx compared to cshtml is that you can view and edit the page itself (WUSIWYG kind of) using the design tab. With cshtml files you might as well use notepad to edit your html page. You are working "in the dark".

Is Cshtml a Razor page?

cshtml file indicates that the file is a Razor Page.


2 Answers

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))
like image 191
abdulbasit Avatar answered Sep 17 '22 12:09

abdulbasit


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.

like image 28
sergioadh Avatar answered Sep 18 '22 12:09

sergioadh