Strange behaviour in Partial views override

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
8 年 前
Lets say you develop a module and want to override some administration views.
In my case i want to override

\Administration\Views\Product\_CreateOrUpdate.SpecificationAttributes.cshtml

Step 1. Register in MyModule my view engine
    
public class RouteProvider : IRouteProvider
    {
        public void RegisterRoutes(RouteCollection routes)
        {
            ViewEngines.Engines.Insert(0, new MyViewEngine());
        }
    }


Step 2. MyViewEngine
public class MyViewEngine : RazorViewEngine
    {
        public MyViewEngine()
        {
            ViewLocationFormats = new string[]
            {
                "~/Plugins/<my module>/Views/{1}/{0}.cshtml",
                "~/Plugins/<my module>/Views/Shared/{0}.cshtml"
            };

            PartialViewLocationFormats = new string[]
            {
                "~/Plugins/<my module>/Views/{1}/{0}.cshtml",
                "~/Plugins/<my module>/Views/Shared/{0}.cshtml"
            };

            AreaPartialViewLocationFormats = PartialViewLocationFormats;
            AreaViewLocationFormats = ViewLocationFormats;

        }
    }


Step 3. Result
I was not able to override _CreateOrUpdate.SpecificationAttributes.cshtml with my customized view inside mymodule.
it is keep using the default

\Administration\Views\Product\_CreateOrUpdate.SpecificationAttributes.cshtml


Is it because

1. The view is being called inside another view?

product\_CreateOrUpdate.cshtml

@helper TabSpecificationAttributes()
{
    @Html.Partial("_CreateOrUpdate.SpecificationAttributes", Model)
}


2. Because "Nop.Web.Framework\Themes\ThemeableVirtualPathProviderViewEngine.cs"

function "GetPath"

has this hack line 138 ?

//little hack to get nop's admin area to be in /Administration/ instead of /Nop/Admin/ or Areas/Admin/
if (!string.IsNullOrEmpty(areaName) && areaName.Equals("admin", StringComparison.InvariantCultureIgnoreCase))
            {
                var newLocations = areaLocations.ToList();
                newLocations.Insert(0, "~/Administration/Views/{1}/{0}.cshtml");
                newLocations.Insert(0, "~/Administration/Views/Shared/{0}.cshtml");
                areaLocations = newLocations.ToArray();
            }


Is there a workaround?
8 年 前
increase the order in routeprovider.cs

public int Priority
        {
            get
            {
                return 0;
            }
        }
8 年 前
Thanks i will try it...

but i thought MyViewEngine is in the first place of all others and it will grab my view for sure
8 年 前
Do you try

public string AreaName
        {
            get
            {
                return "Admin";
            }
        }

at your route.
8 年 前
sina.islam wrote:
Do you try

public string AreaName
        {
            get
            {
                return "Admin";
            }
        }

at your route.




If you try this at route then

if (!string.IsNullOrEmpty(areaName) && areaName.Equals("admin", StringComparison.InvariantCultureIgnoreCase))

condition will be fulfill and the inner code will execute.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.