Can CSHTML files run directly?

17,969

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.

Share:
17,969
VSB
Author by

VSB

Updated on June 11, 2022

Comments

  • VSB
    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
      Ciaran Gallagher about 10 years
      When 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
    tulde23 about 10 years
    cshtml files are intended to be returned from a controller action and never directly invoked
  • VSB
    VSB about 10 years
    So what type of files can call them? I have no good understanding of MVC model in ASP.Net.
  • Ciaran Gallagher
    Ciaran Gallagher about 10 years
    The 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
    tulde23 about 10 years
    Cshtml files are the "views" of MVC therefore they are only accessed by a controller. Does that make sense?
  • Ciaran Gallagher
    Ciaran Gallagher about 10 years
    Might be a good idea to start here before delving any deeper into MVC: asp.net/mvc/tutorials/older-versions/overview/…
  • VSB
    VSB about 10 years
    May 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
    Mike Brind about 10 years
    I 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.