Reputation: 1243
I am interested in changing the structure folder. I have read many articles, but I have not found the solution.
I want to do so to distribute the files and folders on thematic folders. I have created a base class BaseViewEngine from RazorViewEngine
public class BaseViewEngine : RazorViewEngine
{
public BaseViewEngine()
{
MasterLocationFormats = new[]
{
"~/Themes/My/master.cshtml"
};
ViewLocationFormats = new[]
{
"~/Modules/{1}/{0}.cshtml"
};
PartialViewLocationFormats = new[]
{
"~/Blocks/{0}.cshtml"
};
}
}
But it is not working.
Control is primitive. Only for test
public class HomeController : Controller
{
public ActionResult Index()
{
var test = new Test { Text = "Hello" };
return View(test);
}
}
And View
@model DemoModules.Test
<h2>Index</h2>
But when I run project. I Get error
CS0103: The name of the 'model' does not exist in the current context
About structure folder, see the source of subject matter
Upvotes: 13
Views: 2602
Reputation: 100312
You don't really have to implement a new engine to change the paths, you can just register them as you want:
private static void RegisterViewEngines(ICollection<IViewEngine> engines)
{
engines.Clear();
engines.Add(new RazorViewEngine
{
MasterLocationFormats = new[] { "~/Themes/My/master.cshtml" },
ViewLocationFormats = new[] { "~/Modules/{1}/{0}.cshtml" },
PartialViewLocationFormats = new[] { "~/Blocks/{0}.cshtml" },
});
}
protected void Application_Start()
{
RegisterViewEngines(ViewEngines.Engines);
}
For reference, the default paths are as follows (not including Areas):
ViewLocationFormats = new [] {
"~/Views/{1}/{0}.cshtml",
"~/Views/{1}/{0}.vbhtml",
"~/Views/Shared/{0}.cshtml",
"~/Views/Shared/{0}.vbhtml"
};
MasterLocationFormats = new [] {
"~/Views/{1}/{0}.cshtml",
"~/Views/{1}/{0}.vbhtml",
"~/Views/Shared/{0}.cshtml",
"~/Views/Shared/{0}.vbhtml"
};
PartialViewLocationFormats = new [] {
"~/Views/{1}/{0}.cshtml",
"~/Views/{1}/{0}.vbhtml",
"~/Views/Shared/{0}.cshtml",
"~/Views/Shared/{0}.vbhtml"
};
Upvotes: 8
Reputation: 76198
Check out this post: http://weblogs.asp.net/imranbaloch/archive/2011/06/27/view-engine-with-dynamic-view-location.aspx
Hope this helps.
Upvotes: 7
Reputation: 16174
Take a look at the web.config file in the default Views folder. There's some stuff in there that is required for Razor views to work - particularly the base class for views and the namespaces that will be used to compile the view.
Upvotes: 2