- Create a new MVC applictaion
- Open /Views/Shared/Site.Master
- <script src=”/Scripts/script.js” type=”text/javascript”></script>
- This doesn’t point to anything yet
- Open Global.asax
- Add a MapRoute
- routes.MapRoute(“Javascript”, “Scripts/{scriptFile}.js”, new { controller = “Scripts”, action = “Index”, scriptFile = “script” });
- Now /Scripts/script.js points to Script/Index
- Add a MapRoute
- Add a view for CssController/Index
- <%= Url.Content(“~/Content/test.jpg”) %>
- Change contenttype in page directive of Views/Scripts/Index.aspx to “text/javascript”
- That should do it.
Adding strongly typed objects to javascript is now simple to achieve and instead of adding javascript to the <head> in your master page you can just have a dynamically generated and cached .js file.
You may download a sample MVC2 project here.