1

How to deploy cshtml files in asp.net core? If I publish my asp.net core project the cshtml doesn't get published. How to run the cshtml file directly in Chrome?

Here's some code to explain this further

My Program.cs file contains

public static void Main(string[] args)
{
    //since zoho can pass only 10 parameters in one webhook we are splitting into two updates
    //update1
    UpdateClassBoatFromZohoModel upd = new UpdateClassBoatFromZohoModel();
    upd.OnGet();
    //update2
    UpdateClassBoatFromZohoModel2 upd2 = new UpdateClassBoatFromZohoModel2();
    upd2.OnGet();

    //CreateWebHostBuilder(args).Build().Run();
}

Now each of these files UpdateClassBoatFromZoho.cshtml and UpdateClassBoatFromZoho2.cshtml are to be served in the browser with different querystring parameters. How to do that?

Yehor Androsov
  • 4,885
  • 2
  • 23
  • 40
  • Hi, can you provide some extra information, or code examples? – Michael Oct 16 '19 at 10:26
  • When I publish it just converts everything to a dll. I want to run the cshtml files separately in browser. How to do that? – Partha Mandayam Oct 16 '19 at 10:35
  • I suppose you could say a .cshtml file is analogous to a .cs class file: in that the file itself is not a 'static file' that is published, but used by compiling into the assembly. Rather than treat as a static file or resource, you need to expose the .cshtml file as a view or razor page etc. – Michael Oct 16 '19 at 10:53
  • @Michael how to do that? – Partha Mandayam Oct 17 '19 at 10:30
  • I want to open the cshtml file in google chrome and pass parameters in a querystring which the cshtml file parses using queryhelpers. eg. abc.cshtml?x=1&y=2&z=3 etc... – Partha Mandayam Oct 17 '19 at 10:32
  • The easiest way I know is to create a simple ASP.NET Core MVC app: where the 'Model' represents your data class, the 'View' (page) is your .cshtml file, and the 'Controller' includes how logic is processed to access the View; with the app containing additional routing configuration that defines how the controller is called, including parameters. Key to this is that .cshtml file/s can be edited in the project, but not accessed like static files. – Michael Oct 17 '19 at 13:58
  • my asp.net core application contains two cshtml files. I want to serve them individually in browser. How to do that. I have different querystring parameters for both the cshtml files and I want to open each in a browser and parse the querystring parameters and insert into database. How to do this? – Partha Mandayam Oct 18 '19 at 05:27
  • There is a lot to unpack in your request - if you are just starting your project, it is worth your time to Google an MVC or Razor Pages tutorial that will guide you through the steps, and the example will probably help you to see where the .cshtml files fit into the project, along with routing and db access - good luck! – Michael Oct 18 '19 at 10:54
  • I'm not just starting my project. I have read about razor and how to create an asp.net core application. I've also read about asp.net core application deployment to IIS. But I've not been able to find how to serve individual cshtml pages in the browser. Please help. – Partha Mandayam Oct 18 '19 at 11:00
  • Short answer - you don't serve .cshtml files! In an MVC app, you serve a 'View', which is written as a .cshtml file. In a Razor Pages app, you serve a 'Page' which is written as a .csthml file. So approach from the concept that you need to create a View or a Page. For passing parameters, this is regards 'routing' in the application. There is so much to explain, and it is much easier if you search using those terms, does this help? – Michael Oct 18 '19 at 11:05
  • Yes in my app the cshtml files are pages. They are under the pages folder. so from the final application how do I access individual cshtml pages. I'm passing parameters in the querystring not routing. – Partha Mandayam Oct 18 '19 at 11:32
  • How to access a page is determined by the 'route' configuration in the application. Please try the following to if this helps https://learn.microsoft.com/en-us/aspnet/core/razor-pages/razor-pages-conventions?view=aspnetcore-3.0 – Michael Oct 18 '19 at 11:35

2 Answers2

1

Precompilation of pages/views is the default behaviour. It is possible to skip this step and to publish the raw .cshtml files, resulting in pages/views that are updateable in a similar way to classic ASP or the ASP.NET Web Pages frameworks. In other words, you can make changes to the .cshtml files and then copy them to the web server while the application is running, and the new content will take effect immediately.

If you would like to adopt this approach, add an MvcCompileOnPublish node to your .csproj file, with the value set to false:

<PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
</PropertyGroup>

This will result in a Pages folder containing the content pages and a refs folder containing the libraries required for the application:

enter image description here

fingers10
  • 6,675
  • 10
  • 49
  • 87
-1

You can't because that's not how any of this works. The cshtml files cannot be run on their own. They are not served, for one, and they contain pre-processed code that only works in conjunction with the rest of the ASP.NET Core request pipeline. Even if you could access them directly, they wouldn't be anything but a text file (i.e. a web browser would have no idea what to do with it).

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • But in a google search it says "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.Mar 15, 2014" – Partha Mandayam Oct 17 '19 at 10:34
  • That's for ASP.NET, *not* ASP.NET Core, and Web Pages were a failure anyways. No body used it and it didn't get development focus from Microsoft. – Chris Pratt Oct 17 '19 at 11:26
  • Razor Pages are close in concept, but you're still not calling the page directly. It's a route that is based partially on the filesystem path, but not entirely. – Chris Pratt Oct 17 '19 at 11:29
  • I want to open the cshtml file in google chrome and pass parameters in a querystring which the cshtml file parses using queryhelpers. eg. abc.cshtml?x=1&y=2&z=3 etc. – Partha Mandayam Oct 17 '19 at 12:07
  • You *can't*. Not possible. Not going to happen. – Chris Pratt Oct 17 '19 at 12:24
  • my asp.net core application contains two cshtml files. I want to serve them individually in browser. How to do that. I have different querystring parameters for both the cshtml files and I want to open each in a browser and parse the querystring parameters and insert into database. How to do this? – Partha Mandayam Oct 18 '19 at 05:26
  • so how to parse querystring in asp.net core? – Partha Mandayam Oct 18 '19 at 05:31
  • I have read the docs. I cannot find any doc showing how to serve individual cshtml pages in an asp.net core application. Please point me to one. – Partha Mandayam Oct 19 '19 at 08:35
  • No. I mean read the docs from the beginning in their entirety, so you learn how the framework actually works. There's no docs on serving individual cshtml pages, because again and for the last time: you can't do that and it's not how this works. Drop that idea. Learn the framework. – Chris Pratt Oct 19 '19 at 09:33
  • But in asp.net we can serve individual aspx then why not individual cshtml pages? Are you saying I can only access one cshtml page for an asp.net core website. That seems very limiting – Partha Mandayam Oct 23 '19 at 11:46
  • Because they're not aspx. Totally different thing. That Web Forms way of doing things was not good, anyways, so move on. – Chris Pratt Oct 23 '19 at 11:48
  • so how would i call two cshtml pages in individual calls in a browser and pass different querystring parameters each time? – Partha Mandayam Oct 24 '19 at 09:21
  • are you saying then that in asp.net core I can only serve one page? How do I directly call multiple pages? – Partha Mandayam Oct 31 '19 at 10:57
  • @ChrisPratt I think it's important to clarify your answer. The OP asked two questions - (1) how to deploy individual cshtml files and (2) how to call them directly. (1) is possible per the other answer, (2) is not possible. – Howiecamp Oct 05 '20 at 16:32
  • To clarify further it is only possible if you explicitly turn off pre-rendering, which is not recommended. Otherwise, the view is compiled, and deploying the cshtml does nothing. – Chris Pratt Oct 05 '20 at 16:59
  • are you sure? but its possible in dotnet 6 by change setting in cproj file ; check following https://stackoverflow.com/questions/46255990/publish-the-views-with-asp-net-core – R.Akhlaghi May 01 '23 at 07:54