[.NET Core] Render Razor View to string in .NET Core

    之前在MVC5常用的Function,從Controller Render 某一個Partial View的Html來使用。在MVC6本想說將舊程式copy回來就可以用了,但事實上方法簽章不太一樣,所以需作些調整。

原程式-MVC 5

image

新程式-MVC 6

image

差異說明:

  1. ICompositeViewEngine,在建構式須先注入。
  2. 取得預設的Action改由ActionDesciptor取得
  3. 沒有FindPartialView,改成FindView,最後一個參數boolean決定Full View or Partial View
  4. 使用非同步Render

程式片段




參考來源


http://stackoverflow.com/questions/32558941/render-razor-view-to-string-in-asp-net-core

這個網誌中的熱門文章

IIS 設定只允許特定IP進入