本文介绍了注册RazorViewEngine只为C#(仅适用于.cshtml文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只使用 RazorViewEngine 对我的ASP.NET MVC 3应用程序之一,我清除W​​eb窗体视图引擎出具有下列里面<$ C $ code C>的Application_Start 的方法,我的的Global.asax.cs 文件

I am only using RazorViewEngine on one of my ASP.NET MVC 3 applications and I cleared Web Forms view engine out with following code inside Application_Start method of my Global.asax.cs file

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

我决定去看看实的东西,这样我可以就此满足两线 - code-努力,我尝试呈现一个不存在的局部视图,我得到了这样的结果:

I decided to see something solid so that I could be satisfied with this two-line-code-effort and I try to render a partial view which does not exist and I got this result :

局部视图'_ResortMapPartialView没有被发现或没有意见
  引擎支持搜索到的位置。以下部位:
  搜索:
  〜/地区/住宿/查看/度假/ _ResortMapPartialView.cshtml
  〜/地区/住宿/查看/度假/ _ResortMapPartialView.vbhtml
  〜/地区/住宿/查看/共享/ _ResortMapPartialView.cshtml
  〜/地区/住宿/查看/共享/ _ResortMapPartialView.vbhtml
  〜/查看/度假/ _ResortMapPartialView.cshtml
  〜/查看/度假/ _ResortMapPartialView.vbhtml
  〜/查看/共享/ _ResortMapPartialView.cshtml
  〜/查看/共享/ _ResortMapPartialView.vbhtml

看起来更好一点。现在看起来比以前少的项目。但仍与 .vbhtml 扩展名的文件都让我不适应的。

It looks a little better. Now it looks for less items than before. But still the files with .vbhtml extensions are making me unconformable.

问题是:我们怎样才能摆脱他们?

The question is that : How can we get rid of them?

推荐答案

我的建议是重写 RazorViewEngine 定义为以下内容仅包含 CSHTML 文件。

My suggestion would be to override the RazorViewEngine definitions for the following to only include cshtml files.


  • AreaViewLocationFormats

  • AreaMasterLocationFormats

  • AreaPartialViewLocationFormats

  • ViewLocationFormats

  • MasterLocationFormats

  • PartialViewLocationFormats

  • FileExtensions

这是简单的例子:

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
   }

}

请参阅我的回答哪些。同样的原则也适用。您需要注册这个修改后的视图引擎( CSHtmlViewEngine )的 ApplicationStart

See my answer which 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());

这篇关于注册RazorViewEngine只为C#(仅适用于.cshtml文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 18:54