r/dotnet•
cbung

Question about .cshtml.cs filenames

I noticed in VS recently when monkeying around with a Razor Pages project that the convention seems to be AbcPage.cshtml with a AbcPage.cshtml.cs model to go with it. What this does in the Solution Explorer is nest the model file under the razor file, which seems like a handy thing.

If you were creating any Partial or some other non-Razor Page .cshtml file in your project, normally I would just have PartialModel.cs or whatever. But do you think using the .cshtml.cs convention makes sense for basically any [partial] view model? I'm basically saying I'm not sure what the convention actually is :)

Maybe this is a dumb question but trying to google those search terms was pretty tough lol.

Posted Dec 11, 2021, 9:40 AM UTC

214
Basssiiie•

It's looks like a pattern that comes from WinForms and maybe other older frameworks (WebForms too maybe?), where you had your view and a code-behind. I'm not too familiar with it. The collapse-feature in VS's solution explorer probably stuck around for backwards compatibility.

Note that you can put any file in a collapse under other files by specifying the parent with the DependentUpon XML-tag in the *.csproj for the child file. :)

8
Kajayacht•

Webforms did a similar thing. page.aspx and page.aspx.cs

2
quentech•

The collapse-feature in VS's solution explorer probably stuck around for backwards compatibility.

VS will collapse many different types/names of files together by default - most of them are web app/project specific files that came long after WinForms.

It's far from just a backwards compatibility hold-over.

You can also use <DependentUpon> tags in your csproj files to group collapsing files together however you like (I use this for sets of related code-generating template files).

2
idoru9•
4