Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search for view files in a custom location only for Specified Area in MVC 5

I'm looking to override the 'ViewEngine' for MVC5 in a way that first, it find my pages.. which already i failed.

Second, It only operate on a single Area {root}/{area}/{controller}/{action}/{etc.}

as so far as i googled it, i found several topic and answers, but they didn't fit my need. so i rather to ask it here, maybe i'm wrong with something...

public class CustomAreaViewEngine:RazorViewEngine
{
    public CustomAreaViewEngine()
    {
        var viewLocations = new[]
        {
            "~/App/pages/{1}/{0}.cshtml",
            "~/App/pages/{1}/{0}.vbhtml"
        };
        AreaMasterLocationFormats = viewLocations;
        AreaPartialViewLocationFormats = viewLocations;
        AreaViewLocationFormats = viewLocations;
    }

    public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
    {

        var viewEngineResult = base.FindPartialView(controllerContext, partialViewName, useCache);
        return viewEngineResult;
    }

    public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
    {
        controllerContext.RouteData.Values["controller"] = controllerContext.RouteData.Values["controller"].ToString().ToLower();

        var viewEngineResult = base.FindView(controllerContext, viewName, masterName, useCache);
        return viewEngineResult;
    }
}

The method 'FindView' returns empty [first problem]

Global Config:

AreaRegistration.RegisterAllAreas();
ViewEngines.Engines.Add(new CustomAreaViewEngine()); // Look View Inside App/Pages/{1}/{0}.cshtml/.vbhtml

My Hierarchy

Root
--/App
--/--/pages
--/--/shared (but not that shared)
--/...
--/Area
--/--/View
--/--/--/Controller
--/--/--/View(MVC BASED HIERARCHY, I Don't want to use in most case, and redirect to App)
--/--/--/...
--/--/...
--/Controller
--/View
--/--/...(MVC BASED HIERARCHY)
--/...

EDITS:


EDIT1:


Changes i did due to @James Ellis-Jones answers:

Images: My Route Config: My Route Config Area Provided Route Config: Area Provided Route Config Global Config: Global Config My View Engine: My View Engine

still when i use http://localhost:1422/view/home/index i receive an error, which exist in my other home (View, related to the main controller, not the area controller.) it bring a wrong file.

Another Issue I Figured Out, Which Didn't Worked Too

My namespaces was wrong in last edit, and i changed them, but it didn't worked out too.

namespaces: new[] { "RavisHSB.Areas.View.Controllers" }

EDIT2:


Changes i did due to @CarlosFernández answers:

I add ViewEngines.Engines.Clear(); it somehow went one step ahead. but still doesn't work.

New Global.aspx: My Global.aspx And i face this new error: The new Error

like image 669
deadManN Avatar asked Oct 19 '22 11:10

deadManN


1 Answers

Try to customize this code for your engine

Update #1

 private static readonly List<string> EmptyLocations;

    public MyEngine()
    {


        ViewLocationFormats = new[]
            {
                "~/App/Pages/{1}/{0}.cshtml",
                "~/App/Pages/{1}/{0}.vbhtml"
            };


    }
    protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
    {
        try
        {
            return System.IO.File.Exists(controllerContext.HttpContext.Server.MapPath(virtualPath));
        }
        catch (HttpException exception)
        {
            if (exception.GetHttpCode() != 0x194)
            {
                throw;
            }
            return false;
        }
        catch
        {
            return false;
        }
    }

    public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
    {
        var strArray = new List<string>();
        var strArray2 = new List<string>();


        if (controllerContext == null)
        {
            throw new ArgumentNullException("controllerContext");
        }
        if (string.IsNullOrEmpty(viewName))
        {
            throw new ArgumentException("viewName must be specified.", "viewName");
        }


        var controllerName = controllerContext.RouteData.GetRequiredString("controller");
        var viewPath = "";

        var viewLocation = ViewLocationFormats;
        var masterLocation = MasterLocationFormats;
        var area = "";
        if (controllerContext.RouteData.DataTokens.Keys.Any(x => x.ToLower() == "area"))
        {
            area = controllerContext.RouteData.DataTokens.FirstOrDefault(x => x.Key.ToLower() == "area").Value.ToString();

            viewLocation = AreaViewLocationFormats;
            masterLocation = AreaMasterLocationFormats;
        }

        viewPath = GetPath(controllerContext, viewLocation, area, viewName, controllerName, "TubaSite_View", useCache, strArray);



        var masterPath = GetPath(controllerContext, masterLocation, area, masterName, controllerName, "TubaSite_Master", useCache, strArray2);

        if (!string.IsNullOrEmpty(viewPath) && (!string.IsNullOrEmpty(masterPath) || string.IsNullOrEmpty(masterName)))
        {
            return new ViewEngineResult(this.CreateView(controllerContext, viewPath, masterPath), this);
        }
        if (string.IsNullOrEmpty(viewPath))
        {
            throw new Exception(String.Format("Page Not Found - {0} {1}", masterName, masterPath));
        }
        return new ViewEngineResult(strArray.Union<string>(strArray2));
    }
    public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
    {
        var strArray = new List<string>();
        if (controllerContext == null)
        {
            throw new ArgumentNullException("controllerContext");
        }
        if (string.IsNullOrEmpty(partialViewName))
        {
            throw new ArgumentException("partialViewName must be specified.", "partialViewName");
        }



        var requiredString = controllerContext.RouteData.GetRequiredString("controller");

        var partialViewLocation = PartialViewLocationFormats;
        var area = "";
        if (controllerContext.RouteData.DataTokens.Keys.Any(x => x.ToLower() == "area"))
        {
            area = controllerContext.RouteData.DataTokens.FirstOrDefault(x => x.Key.ToLower() == "area").Value.ToString();
            partialViewLocation = AreaPartialViewLocationFormats.Union(PartialViewLocationFormats).ToArray();
        }

        var partialViewPath = "";

        partialViewPath = GetPath(controllerContext, partialViewLocation, area, partialViewName, requiredString, "TubaSite_Partial", false, strArray);

        return string.IsNullOrEmpty(partialViewPath) ? new ViewEngineResult(strArray) : new ViewEngineResult(CreatePartialView(controllerContext, partialViewPath), this);
    }


    private string GetPath(ControllerContext controllerContext, string[] locations, string area,
            string name, string controllerName,
            string cacheKeyPrefix, bool useCache, List<string> searchedLocations)
    {
        searchedLocations = EmptyLocations;
        if (string.IsNullOrEmpty(name))
        {
            return string.Empty;
        }

        if ((locations == null) || (locations.Length == 0))
        {
            throw new InvalidOperationException("Path not found.");
        }

        var flag = IsSpecificPath(name);
        var key = CreateCacheKey(cacheKeyPrefix, name, flag ? string.Empty : controllerName);
        if (useCache)
        {
            var viewLocation = ViewLocationCache.GetViewLocation(controllerContext.HttpContext, key);
            if (viewLocation != null)
            {
                return viewLocation;
            }
        }
        if (!flag)
        {
            return
                GetPathFromGeneralName(controllerContext, locations, area, name, controllerName, key,
                    searchedLocations);


        }
        return GetPathFromSpecificName(controllerContext, name, key, searchedLocations);
    }



    private static bool IsSpecificPath(string name)
    {
        var ch = name[0];
        if (ch != '~')
        {
            return (ch == '/');
        }
        return true;
    }

    private string CreateCacheKey(string prefix, string name, string controllerName)
    {
        return string.Format(CultureInfo.InvariantCulture,
                ":ViewCacheEntry:{0}:{1}:{2}:{3}", GetType().AssemblyQualifiedName, prefix, name, controllerName);
    }

    private string GetPathFromGeneralName(ControllerContext controllerContext, IEnumerable<string> locations, string area, string name,
            string controllerName, string cacheKey, List<string> searchedLocations)
    {
        if (locations == null) throw new ArgumentNullException("locations");
        if (searchedLocations == null) searchedLocations = new List<string>();

        var virtualPath = string.Empty;
        var locationData =
            locations.Select(
                t =>
                string.Format(CultureInfo.InvariantCulture, t, new object[] { name, controllerName })).ToList();
        foreach (var str2 in locationData)
        {
            if (FileExists(controllerContext, str2))
            {
                virtualPath = str2;
                ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, cacheKey, virtualPath);
                return virtualPath;
            }
            searchedLocations.Add(str2);
        }
        return virtualPath;
    }

    private string GetPathFromSpecificName(ControllerContext controllerContext, string name, string cacheKey, List<string> searchedLocations)
    {
        var virtualPath = name;
        if (!FileExists(controllerContext, name))
        {
            virtualPath = string.Empty;
            searchedLocations = new List<string>() { name };
        }
        ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, cacheKey, virtualPath);
        return virtualPath;
    }
}

and also for the last error you've get you can use this config

<configSections>

    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>

  </configSections>

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.Optimization" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>
like image 180
Mahdi Farhani Avatar answered Oct 21 '22 01:10

Mahdi Farhani