Alex Rivera | Logout

Changing ASP.NET MVC 3 folder structure

Asked 2011-06-13T11:13:59.720
13

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.

Update

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

Edit
Report

1 Answer

8

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"
};
answered 2011-06-13T12:16:08.947

Your Answer