Can CSHTML files run directly?
Solution 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.
Solution 2
By default mvc blocks access to views using an http handler in web.config.
VSB
Updated on June 11, 2022Comments
-
VSB almost 2 years
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?-
Ciaran Gallagher about 10 yearsWhen you build and debug your application, it should get deployed to the ASP.NET Development server. Whilst that server is running, you should be able to access the pages from your browser then. The development server will handle the server code which renders the HTML that is returned to the browser.
-
-
tulde23 about 10 yearscshtml files are intended to be returned from a controller action and never directly invoked
-
VSB about 10 yearsSo what type of files can call them? I have no good understanding of MVC model in ASP.Net.
-
Ciaran Gallagher about 10 yearsThe CSHTML files represent the web pages in your application. When you request a web page from your browser, the page request will be received by the server, and the MVC page life cycle will begin. The CSHTML file is the 'View' for the web page.
-
tulde23 about 10 yearsCshtml files are the "views" of MVC therefore they are only accessed by a controller. Does that make sense?
-
Ciaran Gallagher about 10 yearsMight be a good idea to start here before delving any deeper into MVC: asp.net/mvc/tutorials/older-versions/overview/…
-
VSB about 10 yearsMay i ask a question? Are asp application are sensitive to location or folder? since when i deployed my site to a subfolder of root, asp got some error due to problems in accessing bundled dll files and assemblies however when i deployed site to root folder, problem resolved. I think I should add/change something in web.config to help app to find assemblies when i move whole application to a new folder.
-
Mike Brind about 10 yearsI think you need to post a separate question about your issue., It's almost impossible to diagnose without knowing in detail what you did, what errors you get etc, and comments aren't the best place for that. But in principal, URLs map to file system locations so yes, Razor Web Pages sites are sensitive in that regard to folder structures.