This project is read-only.

Views not being found

Nov 21, 2011 at 8:28 PM

I have a class library project that contains my views AND my controllers. I did not see in the documentation if the controllers were supposed to be in the class library project as well, but I am assuming they are.

I installed RazorGenerator using NuGet and I installed the VS extension. I see the generated .cs file in my project.

I referenced my class library from my MVC project. I run the site and it finds the controller but it cannot find the view.

Nov 21, 2011 at 8:32 PM

You need to install the RazorGenerator.Mvc package (via NuGet) to make this work. The tool simply precompiles files. The package registers a ViewEngine that servers those precompiled views.

Nov 21, 2011 at 8:36 PM

I did use NuGet to install RazorGenerator.MVC in my class library. I also installed the VS extension. I mentioned this in my post. Am I misunderstanding something?

Nov 21, 2011 at 8:58 PM

Sorry about that, didn't quite read the second line correctly.

Could you check if the generated classes derive from WebViewPage or WebViewPage<T>? Our heuristics for detecting what type of generator to use (Mvc \ WebPages \ Template) sometimes gets it wrong. Also we use the value in the PageVirtualPathAttribute to figure out where a view should be. Could you verify if the path's where it's supposed to be?

Nov 21, 2011 at 9:02 PM

No problem.

It is derived from WebViewPage<T>.

public class Index : System.Web.Mvc.WebViewPage<dynamic>

The PageVirtualPathAttribute is: [System.Web.WebPages.PageVirtualPathAttribute("~/Views/PatientChart/Index.cshtml")]

It seems right but I'm not exactly sure how I should verify if it is? In my browser I am going to "http://localhost:4133/PatientChart

Thanks
Nov 21, 2011 at 9:49 PM

Do I need to do something special in the MVC project?

Nov 21, 2011 at 11:09 PM

Those looks perfectly fine to me. What happens when you visit the url? Do you get a 404 or does Mvc complain saying it couldn't find the right view?

Nov 22, 2011 at 2:05 PM

I ended up needing to do the following to the MVC project's Global.asax Application_Start method:

ViewEngines.Engines.Add(new PrecompiledMvcEngine(typeof(PrecompiledMvcViewEngineStart).Assembly));

And then add to the usings section my class library's App_Start namespace 

Aug 29, 2012 at 9:22 PM
Edited Aug 29, 2012 at 10:12 PM

I am having similar problem. I am using MVC 4 and VS 2012 RC/Framework 4.5 RC. The error I am getting is:

The view 'GenericError' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/GenericError.cshtml
~/Views/Shared/GenericError.cshtml
~/Views/Home/GenericError.aspx
~/Views/Home/GenericError.ascx
~/Views/Shared/GenericError.aspx
~/Views/Shared/GenericError.ascx
~/Views/Home/GenericError.vbhtml
~/Views/Shared/GenericError.vbhtml

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: The view 'GenericError' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/GenericError.cshtml
~/Views/Shared/GenericError.cshtml
~/Views/Home/GenericError.aspx
~/Views/Home/GenericError.ascx
~/Views/Shared/GenericError.aspx
~/Views/Shared/GenericError.ascx
~/Views/Home/GenericError.vbhtml
~/Views/Shared/GenericError.vbhtml

 

 

- I created a MVC 4 project Solution as my View Library. In this project I have Views/Shared/GenericError.cshtml. After applying "RazorGenerator" as the Custom tool I got :

[System.CodeDom.Compiler.GeneratedCodeAttribute("RazorGenerator", "1.4.1.0")]    [System.Web.WebPages.PageVirtualPathAttribute("~/Views/Shared/GenericError.cshtml")]   

public class GenericError : System.Web.Mvc.WebViewPage<dynamic>    { 

      public GenericError()        {        }

        public override void Execute()        {

                        #line 1 "..\..\Views\Shared\GenericError.cshtml"   Layout = null; 

....

- I created a separate MVC 4  solution and  I installed RazorGenerator.MVC in the MVC 4 Web App project  that will be using the precompiled view

When installing RazorGenerator.MVC in generates: 

 

[assembly: WebActivator.PostApplicationStartMethod(typeof(iTester.Admin.SD.App_Start.RazorGeneratorMvcStart), "Start")]
namespace Test.App_Start { 

  public static class RazorGeneratorMvcStart {     

  public static void Start() {           

var engine = new PrecompiledMvcEngine(typeof(RazorGeneratorMvcStart).Assembly) {         

      UsePhysicalViewsIfNewer = HttpContext.Current.Request.IsLocal            };
            ViewEngines.Engines.Insert(0, engine);
            // StartPage lookups are done by WebPages.    

        VirtualPathFactoryManager.RegisterVirtualPathFactory(engine);        } 

  }

}

and  the Globals.asax.cs has the following:

 

public class MvcApplication : System.Web.HttpApplication    {

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)       

          filters.Add(new HandleErrorAttribute());   

   }


   public static void RegisterRoutes(RouteCollection routes)     

  {

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(             

               "Default", // Route name             

          "{controller}/{action}/{id}", // URL with parameters   

            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults            );
        }


        protected void Application_Start() 

      {

            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

            RouteConfig.RegisterRoutes(RouteTable.Routes);

            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }   

}

 

 

Am I missing something ?

Thanks in advance for your help

Aug 29, 2012 at 10:17 PM

Did you reference your lib project from the main one? I don't see that mentioned. If that doesn't help, does my test project work for you? If so, you can use that to try to figure out what might be different in yours.

David

Aug 29, 2012 at 10:31 PM
Edited Aug 29, 2012 at 10:31 PM

Sorry I forgot to mention that but Yes, I am adding the viewlibrary.dll as a reference to my main MVC 4 project (The one that consumes the pre-compiled view). I did not try yours but let me do that.

Victor

Aug 29, 2012 at 11:03 PM

I found the problem. I was applying/installing   RazorGenerator.MVC to the wrong project.

Now, It works!

Thanks

Victor

Aug 30, 2012 at 12:07 AM

Ah yes, it goes in the Library project, not the main project. The idea is that the library project is self contained, and the main project doesn't need to do anything special for the library to light up its precompiled views. But I can see that being non-obvious, and hopefully this thread will help others later :)

Aug 30, 2012 at 1:14 AM

Just an FYI - we use the v1 parser for our code generation (the parser used by Mvc 3). So if you're using our code generation components, newer features such as ~/ and conditional attributes would work incorrectly. It's subtle because they are valid v1 constructs but the generated output would no longer be correct. There is a work item to add support for the Razor v2 parser and I should get around to it soonish.

Feb 20, 2013 at 3:48 AM