Tag Archives: ASP.NET MVC

ASP.NET MVC Areas in separate projects/assemblies

Had to implement a webshop section for an ASP.NET MVC website project which was to be reused by another one. Solving it was a bit finicky, so documenting it roughly here in case I need to do it again in the future. Not sure I remember all the steps, but hopefully it’ll be enough to reproduce the effect:P

The main problem here is the way razor views are compiled run-time and that is solved by using the Razor Generator VS extension to pre-compile the views and the belonging RazorGenerator.Mvc Nuget package to enable the web application to find them during run-time.

Don’t know if the solution I ended up with is the best, but it worked fairly great. If you have better/cleaner solutions please leave a comment 🙂

Continue reading ASP.NET MVC Areas in separate projects/assemblies

Use the ASP.NET MVC layout for all Areas

Areas in an ASP.NET MVC web application doesn’t use the layout of your web application by default. The reason (as far as I understand) is that your _ViewStart.cshtml which is usually located in ~/Views/ is only visible by views in that folder and its sub-folders.

So the solution, turns out, was more or less just to move that file up a level.

  1. Move ~/Views/_ViewStart.cshtml to ~/_ViewStart.cshtml.
  2. Cut or copy the following elements from ~/Views/Web.config and merge them into ~/Web.config. I say merge, because configSections for example probably already exists, so put the sectionGroup inside it. Don’t know if the Web.config handles multiple configSections…
      <configSections>
        <sectionGroup name="system.web.webPages.razor" ...>
          ...
        </sectionGroup>
      </configSections>

      <system.web.webPages.razor>
        ...
      </system.web.webPages.razor>

All areas should now use the the same layout as the rest of your application.

Output embedded resources into Razor views

In relation to my ASP.NET MVC Areas in separate projects thing I needed to share some tiny bits of javascript and css between pages in this separate project. Could probably be solved better by looking into the bundling stuff, but as a quick fix I decided to just compile the javascript and css files into the assembly and output them raw into my views. Quick and simple.

  1. Set the Build Action of your js and css file to Embedded Resource
  2. Add this extension method to a static class.
    public static IHtmlString RawResource(this HtmlHelper html, Type type, string resourceName)
    {
        using (var s = type.Assembly.GetManifestResourceStream(resourceName) ?? new MemoryStream())
        using (var r = new StreamReader(s))
        {
            s.Position = 0;
            return new HtmlString(r.ReadToEnd());
        }
    }
  3. Make sure the extension method is available in your view
    @using The.Namespace.Of.My.Static.Class
  4. Use the method in your view
    <script type="text/javascript">
        @Html.RawResource(typeof(AnyClassInAssembyOfEmbeddedResource), "Default.Namespace.Of.Project.Resources.file.js");
    </script>

    <style type="text/css">
        @Html.RawResource(typeof(AnyClassInAssembyOfEmbeddedResource), "Default.Namespace.Of.Project.Resources.file.min.css")
    </style>

Easy peasy. And slightly hackish and ugly… let me know if you have smoother, cleaner bundling approach 😉

If you’re having problems figuring out the right name of your embedded resource, see my post on how to use embedded resources for how to list them all out.