Search code examples
c#asp.net-coreasp.net-core-mvcrazor-pagesasp.net-core-7.0

Why ASP.NET Core JS colocation requires config but CSS colocation does not?


In an ASP.NET Core Razor Pages (or MVC) project, there is css colocation:

Index.cshtml
Index.cshtml.css

I don't recall adding configuration for that. I just create a css file and it would be detected and processed during a build.

One can also use js colocation:

Index.cshtml
Index.cshtml.js

But that requires extra effort in the layout and/or view; for example, in the view:

@section Scripts {
  <script src="~/Pages/Foo/Index.cshtml.js"></script>
}

Why does js colocation require extra effort? Is there a way to avoid that manual step?


Solution

  • The "feature" you are trying to implement (JS isolation in Razor Pages/ MVC views) doesn't actually exist. It doesn't appear that it ever will be because you can achieve isolation in JS using modules: https://github.com/dotnet/aspnetcore/issues/30850