Can CSHTML files run directly?

2020-07-23 09:24发布

问题:

I'm just started asp.net using Razor V2 syntax. While I'm developing in Visual Studio, everything is fine however when i try to run .CSHTML files directly from browser, they do not work.
I want to know are .CSHTML files are intended to run directly or they must be used in other segments of web applications which can not be called by browser directly?

回答1:

Yes - cshtml pages can be run directly. If you build an ASP.NET Web Pages (Razor) site, you can request .cshtml pages directly and they will be served (unless the file name starts with an underscore).

You can build a Razor Web Pages site by going to File » New » Web Site. You can get more information about ASP.NET Web Pages from here: http://www.asp.net/web-pages. It is the recommended starting point for people who are new to ASP.NET development.

[UPDATE] They can also be run directly when used as part of a Razor Pages site which was introduced in ASP.NET Core 2.0.



回答2:

By default mvc blocks access to views using an http handler in web.config.



标签: c# asp.net razor