4

I am currently exporting a page from my project into Excel which will not allow the linking of external content e.g. external CSS.

What I am trying to achieve is a simple way to include a CSS file in my view but to have it called directly from a CSS file which will have automatically been minified by Visual Studio.

So, in my view I have tried this:

<style type="text/css">
   @RenderPage("~/CSS/_ExportStyles.min.css")
</style> 

but doing this returns the following error:

Server Error in '/' Application.

The following file could not be rendered because its extension ".css" might not be supported: "~/CSS/_ExportStyles.min.css".

What I know will work would be to place the CSS in a normal .cshtml file and include that, but I would lose the ability to ".min" the file which keeps the file size small and allows me to automatically strip out comments etc.

6
  • Does @Styles.Render("~/CSS/_ExportStyles.min.css") not work? Commented Jan 29, 2014 at 10:23
  • I'm afraid not @markpsmith that will add a link to the file and not include the contents inline. When you open the Export, you get the error message Problems During Load - Missing file:C:\CSS\_ExportStyles.min.css Commented Jan 29, 2014 at 10:35
  • Does it work if you put the css file in that location, i.e. C:\CSS\ Commented Jan 29, 2014 at 11:05
  • It would do, but `C:` is on my local machine... the problem is that once the file is exported, it looks for the CSS on the local machine and not the remote server. I could use a full file path but I don't want the CSS files getting pulled from the server, I wanted them to be self contained and not dependent on an internet connection. Commented Jan 29, 2014 at 11:20
  • 1
    This might help - The second answer demostrates an extension allowing you to embed the css instead of linking to it. Commented Jan 29, 2014 at 11:44

1 Answer 1

21

So, it turns out that @RenderPage is not what I was looking for...

what I needed is this:

@Html.Raw(File.ReadAllText(Server.MapPath("~/CSS/_ExportStyles.min.css")))

Sign up to request clarification or add additional context in comments.

2 Comments

So, file is re-read from disk with every request?
Although this works, it's a bad idea for performance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.