Does ASP.NET bind in regular html?

My angular application is supported by ASP.NET webapi, where I serve index.html and angular processes everything else from there. I would like to use binding, but I don't see how to do this. Do I need to use a razor (or web form) only for package links? Or is there an option to give the package a given name that I can reference in my src / hrefs?

To clarify, I do not use MVC or Webforms to serve html. You are simply redirected to index.html, and routing is client-side. My package is configured using WebActivator.PostApplicationStartMethod.

+2
web-optimization
Mar 02 '14 at 5:14
source share
1 answer

Firstly, to answer only the question, you can simply use a simple link in your html file

<script src='bundles/mybundle' type='text/javascript' language='text/javascript'></script> 

=> which will include your javascript package on the page

The problem with this approach is that if you modify the * .js file contained in the bundle, the modification will not be visible in the bundle. It's all about "unpacking the package cache", a nice feature of ASP.NET, but accessible only from the razor template ... Obviously, you also restart the pool (but it is rather slow and difficult to automate) :)

To work around this problem, you can define your own ASP.NET MVC controller with the following

 using System.Linq; using System.Web.Mvc; using System.Web.Optimization; namespace Controllers { public class DebugBundlesController : Controller { // GET: debugbundles/mybundle public ActionResult Mybundle() { return InlineBundleJavaScript("~/bundles/mybundle"); } private ActionResult InlineBundleJavaScript(string bundlePath) { //see https://blog.mariusschulz.com/2015/10/25/inlining-css-and-javascript-bundles-with-asp-net-mvc var bundleContext = new BundleContext(HttpContext, BundleTable.Bundles, "~/bundles"); var bundle = BundleTable.Bundles.Single(b => b.Path == bundlePath); var js = bundle.GenerateBundleResponse(bundleContext).Content; return JavaScript(js); } } } 

and you use it like:

 <script src='debugbundles/mybundle' type='text/javascript' language='text/javascript'></script> 

=> now, every time you make changes, the package will be regenerated.

Remember to use this only during development, because you remove almost all the benefits of packages (for example, caching clients and servers)

+1
Dec 29 '15 at 9:20
source share



All Articles