question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IncludeAsync should not default to ".cshtml"

See original GitHub issue

Describe the bug

When you use IncludeAsync along with embed resource provider, it will always append .cshtml to the end of the key. I’m trying to include a CSS file to embed it inside the page, but I can’t, unless it ends in .cshtml.

To Reproduce Steps to reproduce the behavior:

@{ await IncludeAsync("Core.css"); } // tries to include "Core.css.cshtml"

Expected behavior

Should properly include Core.css instead of trying to resolve Core.css.html.

Information (please complete the following information):

  • OS: Win10 x64
  • Platform .NET Core 3.1
  • RazorLight version 2.0-beta9
  • Are you using the OFFICIAL RazorLight package? yes
  • Rider 2020.1.3

Additional context Add any other context about the problem here.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
jzabroskicommented, Jan 4, 2021

FileSystemRazorProject.Extension was marked as virtual, and had a DefaultExtension value, which could be configured via constructor.

EmbeddedResourceRazorProject.Extension was not marked as virtual, and had an explicit default value of “.cshtml”. I suppose you could override this by sub-classing EmbeddedResourceRazorProject and defining a different default in that constructor, but that is very ugly.

End-game is to true these up. Both project types should support constructors where you pass in the default extension, and tests should cover that it is ok to pass in an string.Empty value for default extension. In that case, View.css should work (I hope)

1reaction
jzabroskicommented, Dec 24, 2020

I’m not sure exactly how StyleTagHelper works, so maybe it would also solve the problem.

On-Topic: You’d just define a StyleTagHelper with a custom razorlight-src helper attribute that would let you write:

<style razorlight-src="View.css" />

and it would internally call the equivalent of IncludeFileContentsAsync effectively.

Off-topic: I have various ways I’ve loaded CSS into web pages over the years. By far my favorite conceptually was when I was using RequireJS AMD with the requirejs/text plugin, as it enabled me to build TypeScript components that fully packaged a ViewModel, html template and css in a modular way that avoided including the same CSS multiple times. It also was nice in that it made it clear that a component had some assumptions on how it was styled, so the html piece and css piece had clear contracts with the overall component. This had the added benefit I could create an arbitrary MVC endpoint that would just… load a TypeScript component given some jsonConfig. I personally think moving away from such paradigms is a mistake, but as I get older, I’m less rebellious and more compliant with what everyone else is doing.

@jzabroski thanks for getting back about this!

No problem. I just wish I had more free time to just code for fun and work on these projects. While I use these projects professionally, I never knew people had so many diverse use cases.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add a 'default using' to all cshtml pages?
In Asp.Net I'm able to add default stuff for controls to the web.config, hopefully it can be done for MVC.Net as well.
Read more >
Layout in ASP.NET Core
Learn how to use common layouts, share directives, and run common code before rendering views in an ASP.NET Core app.
Read more >
Why You Should Use View Components in ASP.NET Core
Learn why you should use View Components — and not Partial Views — in your ASP.NET Core projects with this hands-on example.
Read more >
Setting a Custom Default Page in ASP.NET Core Razor ...
We can set a custom default landing page for our ASP.NET Core Razor Pages apps! Here's how.
Read more >
Partial Pages in a Razor Pages application
Partial pages are cshtml files that do not take part in routing. ... The default registered search paths are Pages/Shared (from ASP.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found