找不到参考项目中的视图 [英] Cannot find view located in referenced project

查看:69
本文介绍了找不到参考项目中的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的解决方案包含2个项目:

My solution consist of 2 projects:

  • MyApp.Rest
  • MyApp.Core

Core使用Razor模板生成电子邮件和报告.Rest仅是WebAPI,并引用Core.其余的具有启动文件,在该文件中进行Razor配置.将来,Core也将被其他项目使用.

Core uses Razor templating to generate emails and reports. Rest is a WebAPI only and references Core. Rest has the startup file, where Razor configuration takes place. Core will also be used by other projects in the future.

问题是,即使我已将输出目录添加为剃须刀的FileProvider,并且模板已复制到输出目录,我仍无法使视图引擎定位视图文件.

The problem is I am not able to make the view engine to locate view files even though I have added the output directory as a FileProvider for razor and the template has been copied to the output directory.

输出目录:

MyApp.Code.dll
MyApp.Rest.dll
RazorTemplates
 -> Template1.cshtml

Startup.cs

Startup.cs

services.AddMvc()
    .AddApplicationPart(typeof(MyApp.Core.RazorViewRenderer).GetTypeInfo().Assembly)
    .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
    .AddDataAnnotationsLocalization();

services.Configure<RazorViewEngineOptions>(o => {
    o.ViewLocationFormats.Add("/RazorTemplates/{0}" + RazorViewEngine.ViewExtension);
    o.FileProviders.Add(new PhysicalFileProvider(AppContext.BaseDirectory));
});

RazorViewRenderer.cs:

RazorViewRenderer.cs:

public async Task<string> RenderAsync<TModel>(string name, TModel model) {
    var actionContext = GetDefaultActionContext();
    var viewEngineResult = _viewEngine.FindView(actionContext, "Template1", true);

    if (!viewEngineResult.Success) {
        throw new InvalidOperationException($"View '{name}' cannot be found."); //Craches here.
    }

    var view = viewEngineResult.View;
    using (var output = new StringWriter()) {
        var viewContext = new ViewContext(actionContext, view,
            new ViewDataDictionary<TModel>(
                metadataProvider: new EmptyModelMetadataProvider(),
                modelState: new ModelStateDictionary()) {
                Model = model
            },
            new TempDataDictionary(
                actionContext.HttpContext,
                _tempDataProvider),
            output,
            new HtmlHelperOptions());

        await view.RenderAsync(viewContext);
        return output.ToString();
    }
}

注意:RazorLight不是一个选项.它不支持HTML帮助器,也完全不支持本地化.

Note: RazorLight is not an option. It does not support Html helpers and it does not support localization at all.

推荐答案

使用GetView()而不是FindView()解决了我的问题.

Solved my problem using GetView() instead of FindView().

此外,本地化也被打破.我必须使用IViewLocalizer的实现,因为在GitHub上查看Microsoft的代码后,IViewLocalizer使用了IHostingEnvironment中指定的程序集,该程序集设置为MyApp.Rest而不是MyApp.Core.

Also, localization was broken. I had to use my own implementation of IViewLocalizer since looking at Microsoft's code on GitHub, IViewLocalizer uses the assembly specified in IHostingEnvironment, which was set to MyApp.Rest instead of MyApp.Core.

这篇关于找不到参考项目中的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆