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.

wf4
  • 3,727
  • 2
  • 34
  • 45
  • Does @Styles.Render("~/CSS/_ExportStyles.min.css") not work? – markpsmith Jan 29 '14 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` – wf4 Jan 29 '14 at 10:35
  • Does it work if you put the css file in that location, i.e. C:\CSS\ – markpsmith Jan 29 '14 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. – wf4 Jan 29 '14 at 11:20
  • 1
    [This might help](http://stackoverflow.com/questions/14298604/how-to-include-css-styles-inline-in-razor-view) - The second answer demostrates an extension allowing you to embed the css instead of linking to it. – markpsmith Jan 29 '14 at 11:44
  • Thank you for that! looking deeper I found this question http://stackoverflow.com/questions/5314476/how-do-you-include-html-or-asp-file-using-razor/5317313 and found this in one of the answers. `@Html.Raw(File.ReadAllText(Server.MapPath("~/CSS/_ExportStyles.min.css")))` – wf4 Jan 29 '14 at 12:02

1 Answers1

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")))

wf4
  • 3,727
  • 2
  • 34
  • 45