WebApiContrib / WebApiContrib.Formatting.Razor

Web API formatter based on RazorViewEngine
MIT License
24 stars 10 forks source link

Bundles and @Scripts #16

Closed Xiot closed 9 years ago

Xiot commented 10 years ago

Is it possible to get bundles rendered?

Adding the @Scripts.Render("~/bundles/bundlename") in the view throws the following exception

An exception of type 'RazorEngine.Templating.TemplateCompilationException' occurred in RazorEngine.dll but was not handled in user code

Additional information: Unable to compile template. The name 'Scripts' does not exist in the current context

Other compilation errors may have occurred. Check the Errors property for more information.

I tried this in the provided sample project and got the same issue.

EDIT: I was able to get it to resolve by using a custom HtmlTemplateBase and adding a Scripts property that returns a wrapper around the Scripts static class.

It appears though that the output from the Scripts.Render call is being escaped when it is being written out to the page.

panesofglass commented 10 years ago

I don't think RazorEngine works with the bundling and minification library. You are welcome to try and submit a pull request. That would be very helpful!

Xiot commented 10 years ago

Actually, it looks like it does support it.

I got it working in the sample app adding a reference to Microsoft.AspNet.Web.Optimization then including the bundle in the cshtml in the usual way. I also needed to add a using in the page as well.

@using System.Web.Optimization
...
@Styles.Render("~/bundles/css")
@Scripts.Render("~/bundles/jquery")

I'm going to have to play with this a little more in my actual application to see what's going on.

panesofglass commented 9 years ago

@Xiot any further info? I still haven't tried this, but I would love to know what you found and whether or not System.Web.Optimization works with this formatter.

Xiot commented 9 years ago

I haven't touched this in a few months, and recently I've been doing all of my bundling with grunt/gulp. I can re-visit this next week and see what I got working.

panesofglass commented 9 years ago

Don't worry too much. I imagine most people will move toward grunt/gulp as that seems the "official" direction the ASP.NET team is also taking.

pedroreys commented 9 years ago

:+1: @Xiot, @panesofglass can we close this issue then?

Xiot commented 9 years ago

Ya. If I find some time to check this out again I'll let you know On Jan 5, 2015 8:29 PM, "Pedro Reys" notifications@github.com wrote:

[image: :+1:] @Xiot https://github.com/Xiot, @panesofglass https://github.com/panesofglass can we close this issue then?

— Reply to this email directly or view it on GitHub https://github.com/WebApiContrib/WebApiContrib.Formatting.Razor/issues/16#issuecomment-68812977 .