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
This blog has discussed ASP.NET MVC project folder structures like App_Start, App_Data, Content, Controllers, Models, Scripts, Views, and Global.
The App_Data folder of MVC application is used to contain the application related data files like . mdf files, LocalDB, and XML files, etc. The most important point that you need to remember is that IIS is never going to serve files from this App_Data folder.
To add an area to an MVC application, right-click on the project item with in the Solution Explorer window and select Add>Area option as shown below. Now a new prompt will appear, with in give the name of the area like "Department" and click Add button.
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"
};
Check out this post: http://weblogs.asp.net/imranbaloch/archive/2011/06/27/view-engine-with-dynamic-view-location.aspx
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With