2017-08-16 8 views
0

私はRazorをMVC外で使用しています。私は文字列としてビューをレンダリングしたいと思います。ここに私の方法です:Razor .Netコアアプリケーション

私はビューフォルダを作成し、いくつかのビューを貼り付けました。すべてのファイルには[出力ディレクトリにコピー - 常にコピーする]があります。 「/Views/Shared/Email.cshtml」と「/Views//Email.cshtml」:しかし、私は次のここ

var viewResult = _viewEngine.FindView(actionContext, viewName, false); 

を取得していますするViewResultは常にfalseであり、また値を持つプロパティSearchedLocationsを持つプロパティの成功を持っています。何か案は ?

答えて

1

デフォルトのビューの場所はおそらくコントローラが存在しない(とかみそりは、コントローラを見つけることができない)ので、それはあなたのケースで/Views//Email.cshtml/Views/{1}/{0}.cshtml/Views/Shared/{0}.cshtml

です。

あなたは新しいビュー位置エキスパンダーを追加することにより、カスタムの場所を追加することができます。

public class ViewLocationExpander : IViewLocationExpander 
{ 
    public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations) 
    { 
     var locationWithoutController = "/Views/{0}.cshtml"; 
     return viewLocations.Union(new[] { locationWithoutController }); 
    } 

    public void PopulateValues(ViewLocationExpanderContext context) 
    { 
    } 
} 

登録あなたはStartup.csにある縮小:

public void ConfigureServices(IServiceCollection services) 
    { 
     services.AddMvc() 
     .AddRazorOptions(options => 
     { 
      options.ViewLocationExpanders.Add(new ViewLocationExpander()); 
     }); 
    } 
関連する問題