3

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!

gyosifov
  • 3,193
  • 4
  • 25
  • 41
  • I can't replicate this problem using **MVC 5** and **Visual Studio 2013**. I created a new solution and new MVC project, created two controllers: A and B. B has the one method as described, and A is empty and just inherits from B. The two views, `~/Views/B/MyView.cshtml` and `~/Views/B/MyView.cshtml` work as expected. Well-presented question though, so +1. – Rowan Freeman Feb 05 '15 at 23:34
  • I think the problem comes from using RazorGenerator and trying to reuse the views, but I can't find a way around it.. – gyosifov Feb 06 '15 at 06:51
  • I don't think it is a bug with `RazorGenerator`, because `RazorGenerator` maps the View to a physical path `[System.Web.WebPages.PageVirtualPathAttribute("~/Views/B/_MyView.cshtml")]`. Put a break point and see if the MVC calls `B` action method even if you have overwritten it – Catalin Feb 09 '15 at 09:29
  • @RaraituL Controller A inherits from B and doesn't override the action method so - yes it calls, in a way, B's action method, but if I don't use RazorGenerator this type of override works – gyosifov Feb 09 '15 at 10:15
  • @gyosifov: Have you had any luck solving this issue? – Valamas Jun 24 '15 at 01:28

2 Answers2

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.

Valamas
  • 24,169
  • 25
  • 107
  • 177
1

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.

Tom John
  • 783
  • 5
  • 14