0

I am trying to serve some JS and CSS files that are embedded into a DLL, with a solution based on this approach here: http://weblogs.asp.net/imranbaloch/asp-net-bundling-and-minification-and-embedded-resources

so, javascript and css files are embedded and I create bundles for them. My problems start because, having quite a few of them, I need some folder structure to keep order. So the original route

RouteTable.Routes.Insert(0,
new Route("Embedded/{file}.{extension}",
    new RouteValueDictionary(new { }),
    new RouteValueDictionary(new { extension = "css|js" }),
    new EmbeddedResourceRouteHandler()
));

is not enough anymore, so I have changed it to this:

RouteTable.Routes.Insert(0,
new Route("Embedded/{*url}",
    new RouteValueDictionary(new { }),
    new EmbeddedResourceRouteHandler()
));

I also cannot use the extension part because the catch-all part has to be the last one So now if I try to access anything that looks like a file, my route will never be used so I will just get a 404

I have tried replacing the dot with a slash or adding a slash at the end but what I'm after here is a simple solution that will allow me to map urls that look like files to actual files.

I've also searched the web and there seem to be solutions based on UrlRewrite or altering the web.config but: - I would like not to modify the IIS settings for every application to accomodate the library - since it's a library, I would like it to be self contained and developers that use it shouldn't care about these sort of internal issues

So, is there a solution that I can implement in my library for this?

Also worth mentioning is that the original routing had the same issue, it only worked because of

<modules runAllManagedModulesForAllRequests="true" />

in the web.config, which I don't think is a good idea for performance

0

1 Answer 1

1

When you set

  <modules runAllManagedModulesForAllRequests="true" />

this enables all available modules to run against the request. Which, as you mentioned, isn't the best for performance. However, you could add only the module you actually need- in this case the UrlRoutingModule.

You could add this module like this:

  <system.webServer>   
    <modules>
      <remove name="UrlRoutingModule-4.0" />
      <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
    </modules>
  </system.webServer>

If you want an even better way (IMO) to do this, disregard the WebConfig and add it in a AppStart.cs file in your class library.

using Microsoft.Web.Infrastructure.DynamicModuleHelper;

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(AppStart), "PreStart")]
[assembly: WebActivatorEx.PostApplicationStartMethod(typeof(AppStart), "Start")]
namespace EmbeddedPages
{
  public static class AppStart
  {
      private static bool PreStartFired = false;

      public static void PreStart()
      {
          if (!PreStartFired)
          {
              PreStartFired = true;
              DynamicModuleUtility.RegisterModule(typeof(UrlRoutingModule));
          }
      }
  }
}

This adds the UrlRoutingModule into the module stack, and your URL's should now properly resolve. Note: you will need to add WebActivator to your project through nuget.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.