问题
I have a separate project in my solution that contains some Controllers and compiled views. I use those controllers for base classes to other controllers in my MVC application and the views are compiled using RazorGenerator.
Lets say B is Base Controller with non abstract action method SomeAction that returns View("_MyView"). _MyView.cshtml is compiled using RazorGenerator.
Lets say controller A inherits B but doesn't override SomeAction.
I've tried to make another view "~/Views/A/_MyView.cshtml" to override the default one, but it doesn't work. My question is how can I accomplish this?
ADDITIONAL INFO
1) I know that the views by default are searched in that order in those paths
"~/Views/{1}/{0}.cshtml",
"~/Views/{1}/{0}.vbhtml",
"~/Views/Shared/{0}.cshtml",
"~/Views/Shared/{0}.vbhtml"
2) Putting "~/Views/Shared/_MyView.cshtml" does override the view, but not only for controller A, but also for every other controller that inherits B
3) Overriding SomeAction to return base.SomeAction() doesn't work
UPDATE
I have found similar question here, but doing the suggestion nothing happened RazorGenerator Issues
I have posted my own issue here
Thank you in advance!
回答1:
So far my only workaround is to install RazorGenerator on the consumer app and to also set the view _MyView.cshtml as being RazorGenerated. RazorGenator then picks up the correct view.
Another note for other visitors is not to compound the wrong view confusion with the route going to the base controller instead of the the consumer controller. In solving this issue earlier to being able to figure out the actual wrong view was being served by the right controller as the OP and I have an issue with. I have code in my base application_start that removes route duplicates.
回答2:
Anyone else hitting this issue you need to update the RazorGeneratorMvcStart.cs to set PreemptPhysicalFiles = false in the master project. By default this is not the case and the views in the master project with take priority:
var engine = new PrecompiledMvcEngine(typeof(RazorGeneratorMvcStart).Assembly)
{
UsePhysicalViewsIfNewer = HttpContext.Current.Request.IsLocal,
PreemptPhysicalFiles = false
};
ViewEngines.Engines.Add(engine);
This file is App_Start\RazorGeneratorMvcStart.cs. It is also important to Add the engine rather than Insert it. The default is to insert at position 0.
Note: I hit this issue when updating NuGet packages, it seems that the file gets overritten, resetting this back to the default behaviour.
来源:https://stackoverflow.com/questions/28348377/override-view-in-asp-net-mvc-site-not-working