In my razor views, I have:
<link rel="stylesheet" href="css/print-layout.css" media="print" type="text/css" />
Which correctly applies that stylesheet when printing the page.
However, in some cases I'm rendering the view to an HTML string using something like:
var razorViewEngine = new RazorViewEngine();
ViewEngineResult viewResult = razorViewEngine.FindView(context, viewName, "_Layout", false);
// ...
var writer = new System.IO.StringWriter();
var viewContext = new ViewContext(context, viewResult.View, viewData, tempData, writer);
viewResult.View.Render(viewContext, writer);
return writer.ToString();
When doing so, how can I specify the media type, so that the "print-layout.css" stylesheet listed above is applied?
The print stylesheet is applied by the viewing user agent (browser) when printing.
You can change
media="print"
to
media="all"
to have it apply to all media types.
Update:
Why don't you try setting the stylesheet you want applied at Render time?
Try the (RenderPartial
) to pass some data through so that you can conditional use "print" or "all".
Update 2: The HTML output won't differ, only the CSS rules that are applied to the actual rendered page that is viewed by a human. Or are you looking to see what styles are applied to the final rendered page DOM instead of what the HTML looks like?