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
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.
- Move ~/Views/_ViewStart.cshtml to ~/_ViewStart.cshtml.
- 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…
<sectionGroup name="system.web.webPages.razor" ...>
All areas should now use the the same layout as the rest of your application.
- Set the Build Action of your js and css file to Embedded Resource
- Add this extension method to a static class.
HtmlHelper html, Type type, string
resourceName) ?? new
r = new
s.Position = 0;
- Make sure the extension method is available in your view
- Use the method in your view
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.