Is a way I can render CSS/Javascript bundles witho

2019-05-18 16:52发布

问题:

The only thing that my code uses which requires ASP.NET MVC is:

<<< HTML here >>>
<body>
    <<< Some HTML here >>>
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/textview")
    @Scripts.Render("~/bundles/Angular")
    @Scripts.Render("~/bundles/AngularApp")
</body>
</html>

Here's a sample from my BundleConfig.cs

    bundles.Add(new StyleBundle("~/bundles/css").Include(
       "~/Content/css/*.css"
    ));

The ability to render the CSS in debug or release is really good but I want to move completely away from using MVC and just use and index.html page and ASP.NET Web API. Is there a way that I can get similar functionality with rendering bundles without using a controller?

回答1:

You can do two things:

  1. You already create a virtual path to these bundles so you can access them externally.
  2. You can create your html pages using the Razor view engine, and by doing that, get the "strength" of the Razor engine for things like bundles - You simple create a route that will help you server your static files from a single action.

lets say the routing will be:

routes.MapRoute(
                name: "HTMLViewRoute",
                url: "Htmls/{*pathInfo}",
                defaults: new { controller = "HTMLFileRouting", action = "ViewRoute"});

In the route take the pathinfo and find the path to your html file(using Server.MapPath) and return View(filePath,null) since you need no model.