Monday, 30 July 2012

Config Settings into JavaScript using IBundleTransforms

The Problem:

You need to dynamically set a variable in a static JavaScript file.

The variable changes with each target environment (test/production etc) and so needs to be set in the app settings (web.config).


MVC4 comes with out of the box bundling capability for static text content. Read for what bundling is and why it is a good idea.

The .net Bundling feature has been designed to be pluggable so you can roll your own Bundler by implementing the IBundleTransform interface.

This is very neat way of addressing the problem of setting a variable in a static file, as you can handle the logic of replacement in your own IBundleTransform class.

The Solution:

1. Add the configuration variable to your app settings of your web.config.

2. Replace the value of the variable being setting in the script with a token e.g. {{ServerAddress}}.

3. Create a bundle Transform that looks for this token and replaces it with the setting from the app settings. We are dealing with the BundleResponse object here.

4. Initialise the bundle in the RegisterBundles method of your BundleConfig in the App_Start folder, specify the virtual path, the files and folders you want to be bundled. Then add a new instance of your IBundleTransform to the list of the bundle's Transforms.

Now, when you make a request to the virtual path (~/Scripts/AllScripts.js) your JavaScript will be returned both minified and with the configuration specific variable included! The replacement logic only gets called once too, as .net will cache the output.

What's more, the MyBundleTransform class is nicely contained and Unit Testable.

Hope this helps!

Thursday, 12 July 2012

Method Not Allowed 405 on IIS7 Website eg PUT, DELETE etc

If you are working with a .net MVC4 WebApi project that needs to provide CRUD capabilities then your the standard Http Methods your site will need to accept are GET, POST, PUT, DELETE.

I had trouble getting my site to accept PUT and DELETE methods when hosting my site locally with IIS7. I am hosting the application under its own website, this lets me change the settings for the particular site using the IIS7 interface. Here are the steps I took to correct the problem:

Begin in IIS7 by highlighting your website, then choosing Handler Mappings from the available options:

Then select WebDAV from the list of Mappings.

Click Request Restrictions.

Then switch to the Verbs tab and highlight All Verbs.

Restart the application in IIS and hey presto! PUT and DELETE enabled (as well as all HTTP methods). If you want to more conservative about which methods are supported then use the option beneath All Verbs.This results in the changes to your Web.config. Inside your <system.web> section goes this snippet: (You don't actually have to add this - IIS7 will have added this itself)

  <remove name="WebDAV" />
  <add name="WebDAV" path="*" verb="*" modules="WebDAVModule" resourceType="Unspecified" requireAccess="None" />

Hope this will help someone out!

P.s. I found WFetch to be quite a useful tool in debugging http requests.