Question

I recently upgraded from MVC2 to MVC3. We used to use the Spark view engine and I'm trying to start migrating over to Razor. So far the upgrade to MVC3 was successful. I upgraded the Spark view engine as well because I needed to.

The issue is that I am able to render both Spark and Razor views successfully but for some reason MVC is looking for Spark files in one location and Razor in another. It's as if Razor isn't factoring in my areas properly but Spark is.

Output:

<pre>
The view 'index' or its master was not found or no view engine supports the searched locations. The following locations were searched:

~/Areas/Live/Views/multimedia/index.aspx
~/Areas/Live/Views/multimedia/index.ascx
~/Areas/Live/Views/Shared/index.aspx
~/Areas/Live/Views/Shared/index.ascx
~/Views/multimedia/index.aspx
~/Views/multimedia/index.ascx
~/Views/Shared/index.aspx
~/Views/Shared/index.ascx
~/Areas/Live/Views/multimedia/index.cshtml
~/Areas/Live/Views/multimedia/index.vbhtml
~/Areas/Live/Views/Shared/index.cshtml
~/Areas/Live/Views/Shared/index.vbhtml
~/Views/multimedia/index.cshtml
~/Views/multimedia/index.vbhtml
~/Views/Shared/index.cshtml
~/Views/Shared/index.vbhtml
Live\~\Areas\Live\Views\multimedia\index.spark
Live\~\Areas\Live\Views\Shared\index.spark
Live\multimedia\index.spark
Live\Shared\index.spark
Live\~\Areas\Live\Views\multimedia\index.shade
Live\~\Areas\Live\Views\Shared\index.shade
Live\multimedia\index.shade
Live\Shared\index.shade
~\Areas\Live\Views\multimedia\index.spark
~\Areas\Live\Views\Shared\index.spark
multimedia\index.spark
Shared\index.spark
~\Areas\Live\Views\multimedia\index.shade
~\Areas\Live\Views\Shared\index.shade
multimedia\index.shade
Shared\index.shade
</pre>

If I move my .cshtml file to where MVC wants it to be it will work but that's not going to cut it. Why would the two engines be looking in slightly different spots?

Was it helpful?

Solution

From what I've read it seems that Razor and Spark look for views in different ways (when using areas anyway). I'm not entirely sure if this is true in general or if there's something about this implementation I'm working on that is different. I managed to solve my problem by extending the Razor engine to follow the same pattern our project does.

  public class CustomRazorViewEngine : RazorViewEngine
  {
      public CustomRazorViewEngine(): base()
      {
          AreaMasterLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/%1/{0}.cshtml",
                             "~/Areas/{2}/Views/{1}/%1/{0}.vbhtml",
                             "~/Areas/{2}/Views/Shared/%1/{0}.cshtml",
                             "~/Areas/{2}/Views/Shared/%1/{0}.vbhtml" };

          AreaPartialViewLocationFormats = new string[] { "~/Views/{2}/Shared/{0}.cshtml" };

          AreaViewLocationFormats = new string[] { "~/Views/{2}/{1}/{0}.cshtml",
                       "~/Views/{2}/Shared/{0}.cshtml" };

          ViewLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml",
                       "~/Views/Shared/{0}.cshtml" };

          MasterLocationFormats = new string[] { "~/Views/{1}/%1/{0}.cshtml",
                         "~/Views/{1}/%1/{0}.vbhtml",
                         "~/Views/Shared/%1/{0}.cshtml",
                         "~/Views/Shared/%1/{0}.vbhtml" };

          PartialViewLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml",
                          "~/Views/{1}/{0}.vbhtml",
                          "~/Views/Shared/{0}.cshtml",
                          "~/Views/Shared/{0}.vbhtml" };

          FileExtensions = new string[] { "cshtml", "vbhtml" };

      }

  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top