【问题标题】:Custom Razor View Engine in MVC 6MVC 6 中的自定义 Razor 视图引擎
【发布时间】:2015-02-12 05:31:48
【问题描述】:

我正在 .NET 5 MVC 6 中进行一些原型设计,并试图找出在哪里注册我的自定义剃须刀视图引擎。 MVC 6 不再有一个 global.asax 文件来注册自定义对象。它确实有一个 startup.cs 文件,我认为它是添加它的地方,但我不确定这是否是执行此操作的正确位置以及调用什么方法。

我目前有类似的自定义剃须刀视图引擎

public class MyCustomerViewEngine : RazorViewEngine
{
    public MyCustomerViewEngine()
    {
        //My custom view stuff
    }
}

任何帮助将不胜感激。

【问题讨论】:

    标签: c# asp.net-core razor .net-core asp.net-core-mvc


    【解决方案1】:

    Startup.cs文件中

    转到ConfigureServices 方法。

    services.AddMvc(); // Replace this with following
    
    services.AddMvc().Configure<MvcOptions>(options =>
    {
        options.ViewEngines.Add(new MyCustomerViewEngine ());
    });
    

    【讨论】:

    • 确保包含using Microsoft.AspNet.Mvc。默认情况下它就在那里,但如果你做了一个删除和排序使用,它就会被删除。 :(
    猜你喜欢
    • 2011-03-11
    • 1970-01-01
    • 2011-02-27
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    相关资源
    最近更新 更多