Register RazorViewEngine for C # only (.cshtml files only)

I use only RazorViewEngine in one of my ASP.NET MVC 3 applications, and I cleared the web form viewer using the following code inside the Application_Start method of my Global.asax.cs file

 ViewEngines.Engines.Clear(); ViewEngines.Engines.Add(new RazorViewEngine()); 

I decided to see something solid to be satisfied with this two-line code, and I am trying to make a partial representation that does not exist, and I got this result:

Partial view '_ResortMapPartialView' not found or missing engine supports found locations. The following places were what you were looking for: ~ / Regions / Accommodation / Views / resort / _ResortMapPartialView.cshtml ~ / Regions / Accommodation / Views / resort / _ResortMapPartialView.vbhtml ~ / Regions / Accommodation / Views / Shared / _ResortMapPartialView.cshtml ~ / Regions / Accommodation /Views/Shared/_ResortMapPartialView.vbhtml ~ / Views / resort / _ResortMapPartialView.cshtml ~ / Views / resort / _ResortMapPartialView.vbhtml ~ / Views / Shared / _ResortMapPartialView.cshtml ~ / Views / Shappialial.cshtml ~ / Views / SharedPartialView.cshtml ~ / Views / Shared

It looks a little better. Now he is looking for fewer items than before. But still, files with the .vbhtml extensions make me disagree.

The question is: how can we get rid of them?

+2
source share
2 answers

My suggestion was to override the RazorViewEngine definitions for the next to include only cshtml files.

  • AreaViewLocationFormats
  • AreaMasterLocationFormats
  • AreaPartialViewLocationFormats
  • ViewLocationFormats
  • MasterLocationFormats
  • PartialViewLocationFormats
  • FileExtensions

A brief example:

 public class CSHtmlViewEngine: RazorViewEngine { public CSHtmlViewEngine() { base.AreaViewLocationFormats= new string[] { "~/Views/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml" }; base.AreaPartialViewLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", }; // All the other LocationFormats listed above will also need to be amended // Don't forget the FileExtensions array } 

}

Check out my answer that talks about overriding these values . The same principle applies. You will need to register this modified ViewEngine ( CSHtmlViewEngine ) in the ApplicationStart method

 ViewEngines.Engines.Clear(); ViewEngines.Engines.Add(new CSHtmlViewEngine()); 
+6
source

Instead of subclassing RazorViewEngine or replacing it directly, you can simply modify the existing RazorViewEngine PartialViewLocationFormats property. This code is in Application_Start:

 System.Web.Mvc.RazorViewEngine rve = (RazorViewEngine)ViewEngines.Engines .Where(e=>e.GetType()==typeof(RazorViewEngine)) .FirstOrDefault(); string[] additionalPartialViewLocations = new[] { "~/Views/[YourCustomPathHere]" }; if(rve!=null) { rve.PartialViewLocationFormats = rve.PartialViewLocationFormats .Union( additionalPartialViewLocations ) .ToArray(); } 
0
source

Source: https://habr.com/ru/post/899016/


All Articles