<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[James Walker - Medium]]></title>
        <description><![CDATA[Musings from James Walker — Enterprise Architect, Tech Evangelist, Founder &amp; Director of Redspa in Tunbridge Wells, Kent, UK and daddy to two gorgeous boys. - Medium]]></description>
        <link>https://medium.com/james-walker?source=rss----93b22a006240---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>James Walker - Medium</title>
            <link>https://medium.com/james-walker?source=rss----93b22a006240---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 03:34:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/james-walker" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Embedding assets in a .NET Assembly]]></title>
            <link>https://medium.com/james-walker/embedding-assets-in-a-net-assembly-b6bee6105a07?source=rss----93b22a006240---4</link>
            <guid isPermaLink="false">https://medium.com/p/b6bee6105a07</guid>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[James Walker]]></dc:creator>
            <pubDate>Fri, 26 May 2017 08:30:54 GMT</pubDate>
            <atom:updated>2017-05-26T08:34:42.447Z</atom:updated>
            <content:encoded><![CDATA[<p>I’ve recently been asked how we go about embedding static assets in some <a href="http://redspa.uk/">Redspa’s</a> core libraries. This is essentially compiling assets into a shared .NET Framework assembly that needs to be used across a number of different projects.</p><p>I haven’t, as yet played with this on .NET Core, so this solution is very much for the .NET Framework and is also a few years old, so there may be better ways to do it!</p><h3>Setting your assets to be embedded</h3><p>The first thing that you need to do is to create a Visual Studio project that will hold all your assets. This could be the same assembly where you have other shared elements, such as common Controller actions and/or Views that you’ve compiled using RazorGenerator.</p><p>The first thing to do is to set the resources you want to be shared to be embedded within the project. The simplest way to do this is to mark the Build Action as an Embedded Resource within Visual Studio:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/708/1*mwWIO19ED2Rqa4hTN9ctjQ.gif" /><figcaption>Setting Assets to Embedded from the Properties Pane</figcaption></figure><p>However, this will become really tedious if you need to embed a whole load of resources, so it’s actually much easier to tell Visual Studio that you want to embed all the resources in a specific folder on compile:</p><p>If you’ve got a lot of embedded resources, it can be a pain to mark every single item as embedded. The alternative is to edit your .csproj file directly and add the following in the BeforeBuild section:</p><pre>&lt;Target Name=&quot;BeforeBuild&quot;&gt;<br>    &lt;ItemGroup&gt;<br>      &lt;EmbeddedResource Include=&quot;**\*.gif;**\*.jpg;**\*.css;**\*.js;**\*.png&quot; /&gt;<br>    &lt;/ItemGroup&gt;<br>  &lt;/Target&gt;</pre><h3>Accessing the embedded resources</h3><p>Once the resources are embedded in your assembly, you can add a method within the assembly to access the resources and stream them to the browser.</p><p>In this instance, I’m going to create an ASP.NET Controller Action to perform the streaming and use reflection to access the embedded resources:</p><pre>public class ResourceController : Controller<br>    {<br>    <br>        public ActionResult Index(string resourceName)<br>        {</pre><pre>        }</pre><pre>    }</pre><p>So far, so simple. The idea here is that the controller action will be called with the resource name passed through as a property.</p><p>Next, we need to find the embedded file in the assembly. When files are embedded in an assembly, they are stored with a reference of the assembly namespace and then the filename with the file path converted to dot notation. It’s also worth noting that any filenames are case sensitive.</p><p>For example, if the file is in the assembly with a default namespace of:</p><pre>Test.EmbeddedResources</pre><p>and a relative file path of</p><pre>Content\Assets\file.png</pre><p>Then the embedded resource would be:</p><pre>Test.EmbeddedResources.Content.Assets.file.png</pre><p>We’re ideally wanting our embedded resources to be able to be called as if they’re ‘real’ file URLs, so we’d expect the resourceName parameter to our controller action to have forward slashes.</p><p>The first thing therefore is to convert any forward slashes to full stops — as this is how they get mapped when embedded and then add on our assembly namespace:</p><pre>resourceName = resourceName.Replace(&quot;/&quot;, &quot;.&quot;);<br>resourceName = &quot;Test.EmbeddedResources.&quot; + resourceName;</pre><p>Now, we can use this reference to the resource to query the assembly using reflection:</p><pre>var resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);</pre><p>This returns a stream reference to the embedded resource that we can just return as the result of our action:</p><pre>return File(resourceStream, contentType);</pre><p>The contentType variable is a string with the Mime type of the file that’s being returned. I’ve got a helper function that works this out for a standard set of files that I’m most likely to use.</p><p>Wrapping it all up, our action now looks like this:</p><pre>public class ResourceController : Controller<br>    {<br>        public ActionResult Index(string resourceName)<br>        {</pre><pre>resourceName = resourceName.Replace(&quot;/&quot;, &quot;.&quot;);<br>            resourceName = &quot;Redspa.MVCLibrary.&quot; + resourceName;</pre><pre>var contentType = GetContentType(resourceName);<br>          </pre><pre>var resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);<br>                <br>                return File(resourceStream, contentType);<br>         }</pre><pre>    }</pre><h3>Routing</h3><p>Finally, we now need to make sure our routing is all setup.</p><p>Within the library project, we need to add a route to point to our controller action. So, in our RouteConfig.cs we add:</p><pre>routes.MapRoute(<br>                name: &quot;ResourceController&quot;,<br>                url: &quot;common/resource/{*resourceName}&quot;,<br>                defaults: new { controller = &quot;Resource&quot;, action = &quot;Index&quot; }</pre><p>We now add a web.config setting to the system.webServer section of main web application that we need to access the shared resource. This allows us to use pick up the URLs with extensions on them and pass them through to the .NET pipeline.</p><pre>&lt;handlers&gt;<br>     &lt;add name=&quot;Embed-Integrated-1.0&quot; path=&quot;common/resource/*.*&quot; verb=&quot;GET&quot; type=&quot;System.Web.Handlers.TransferRequestHandler&quot; preCondition=&quot;integratedMode,runtimeVersionv4.0&quot; /&gt;<br>&lt;/handlers&gt;</pre><p>You should now be able to reference your stored assets using a standard URL</p><pre>&lt;img src=&quot;/common/resource/Content/Assets/file.png&quot; /&gt;</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b6bee6105a07" width="1" height="1" alt=""><hr><p><a href="https://medium.com/james-walker/embedding-assets-in-a-net-assembly-b6bee6105a07">Embedding assets in a .NET Assembly</a> was originally published in <a href="https://medium.com/james-walker">James Walker</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction and Welcome]]></title>
            <link>https://medium.com/james-walker/introduction-f421c1981eec?source=rss----93b22a006240---4</link>
            <guid isPermaLink="false">https://medium.com/p/f421c1981eec</guid>
            <category><![CDATA[microsoft]]></category>
            <dc:creator><![CDATA[James Walker]]></dc:creator>
            <pubDate>Thu, 25 May 2017 21:40:31 GMT</pubDate>
            <atom:updated>2017-05-25T21:29:34.464Z</atom:updated>
            <content:encoded><![CDATA[<p>A quick overview of myself and why I’m here!</p><p>Just to give you a bit of a background — I’m based in Tunbridge Wells, Kent, UK and am Founder and Managing Director of Redspa, a digital agency that I setup seven years ago (www.redspa.uk).</p><p>We’re a smallish team of 10 talented developers and designers specialising in creating bespoke websites, systems, graphics and identities for brands big and small.</p><p>Prior to this, I worked as a freelance website and systems developer for almost 15 years.</p><p>I’ve grown up with the Microsoft tech-stack, having started programming in my first holiday job designing and building Access databases and scripting in VBScript when I was 15. I quickly progressed onto classic Active Server Pages (ASP), ASPX Web Forms and these days I’m firmly embedded in the C# ASP.MVC, WebAPI and ASP.NET Core world. I’m also pushing forward into the modern front-end world of TypeScript, React, Vue, Node and how they all interact.</p><p>On top of this, I’m now very much in the realm of running my own business so in between the techy bits expect a comment, quip and/or rant or two about the trials and tribulations of running a digital agency.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f421c1981eec" width="1" height="1" alt=""><hr><p><a href="https://medium.com/james-walker/introduction-f421c1981eec">Introduction and Welcome</a> was originally published in <a href="https://medium.com/james-walker">James Walker</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>