Decision to support species in single-project areas

I just tried this in specific areas of the project. Therefore, if someone tries this in a solution with several projects, please let us know.

Area support has been added in MVC2. However, the views for your controllers should be in your Views main folder. The solution that I propose here will allow you to keep your specific areas in each area. If your project is structured as shown below with a block being an area.

+ Areas <-- folder + Blog <-- folder + Views <-- folder + Shared <-- folder Index.aspx Create.aspx Edit.aspx + Content + Controllers ... ViewEngine.cs 

Add this code to the Application_Start method in Global.asax.cs. It will clear your current view engines and use our new ViewEngine instead.

 // Area Aware View Engine ViewEngines.Engines.Clear(); ViewEngines.Engines.Add(new AreaViewEngine()); 

Then create a file called ViewEngine.cs and add the code below.

 using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Web.Mvc; namespace MyNamespace { public class AreaViewEngine : WebFormViewEngine { public AreaViewEngine() { // {0} = View name // {1} = Controller name // Master Page locations MasterLocationFormats = new[] { "~/Views/{1}/{0}.master" , "~/Views/Shared/{0}.master" }; // View locations ViewLocationFormats = new[] { "~/Views/{1}/{0}.aspx" , "~/Views/{1}/{0}.ascx" , "~/Views/Shared/{0}.aspx" , "~/Views/Shared/{0}.ascx" , "~/Areas/{1}/Views/{0}.aspx" , "~/Areas/{1}/Views/{0}.ascx" , "~/Areas/{1}/Views/Shared/{0}.aspx" , "~/Areas/{1}/Views/Shared/{0}.ascx" }; // Partial view locations PartialViewLocationFormats = ViewLocationFormats; } protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath) { return new WebFormView(partialPath, null); } protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath) { return new WebFormView(viewPath, masterPath); } } // End Class AreaViewEngine } // End Namespace 

This will allow you to find and use the views you created in your areas.

This is one of the possible solutions that allows me to view views in a specified area. Does anyone else have a different, better, advanced solution?

thanks

+4
source share
2 answers

This solution works well in Mvc2. This is not necessary in Mvc3.

0
source

I apologize for telling you about this, but you are missing something. I currently have a script working out of the box with ASP.NET MVC 2 RC.

I assume that you have all the registration paths and you have the correct web.config files in the browsing area folder?

Perhaps see this transition through , especially the area creation part.

HTHS,
Charles

EDIT: Okay, so you are not happy that you added the extra new { area = "blog' }, null is honest enough, I admit it's nothing ... but what else are you going to do?

What happens when you have two controllers with the same name? One in your root project and one in an area or two controllers with the same name in two different areas? How will this look for the right kind?

Also, I see a problem with your ViewLocationFormats . In all places of the region’s review there is no reference to their region ... for example. ~/Areas/{1}/Views/{0}.ascx - how does he know in which area?

If you suggest that all different kinds of areas and all of them be added to the Areas folder under their controller name, and then found under Views and Views/Shared - I would highly recommend against this ... It will become a mess very quickly.

So where does this leave you? You really need to specify the area when creating the route. It really comes down to the fact that, although it’s insignificant to indicate the area, there is no other way.

+2
source

Source: https://habr.com/ru/post/1300119/


All Articles