I'm trying to use the new Razor SDK to include my views within my class libraries where each class library is an MVC Area. If I include the views with an Areas directory in my class library e.g.
/MyLibrary/Areas/MyLibrary/Views/Home/Index.cshtml
Then it loads fine. However I don't like that I have to place them inside an Areas directory ideally the path would be:
/MyLibrary/Views/Home/Index.cshtml
I'm guessing I would have to use a view location expander to achieve this. However I don't know how to achieve this for a view which is contained within a class library and not within the application. So far I have come up with:
public class AreaViewLocationExpander : IViewLocationExpander {
public virtual IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations) {
return viewLocations.Concat(new[] {
"../{2}/Views/{1}/{0}" + RazorViewEngine.ViewExtension
});
}
public virtual void PopulateValues(ViewLocationExpanderContext context) { }
}
This throws the error:
InvalidOperationException: The view 'Index' was not found. The following locations were searched:
/Areas/MyLibrary/Views/Home/Index.cshtml
/Areas/MyLibrary/Views/Shared/Index.cshtml
/Views/Shared/Index.cshtml
/Pages/Shared/Index.cshtml
/../MyLibrary/Views/Home/Index.cshtml
But I'd imagine even if it did work locally, it wouldn't in production or when the library is packaged up in a NuGet package.
I'd appreciate it if someone could show me how this can be achieved. Thanks