2013-05-05 67 views
0

我知道這看起來像是重複的問題,但在將其標記爲重複之前,請先閱讀整個問題。將部分視圖渲染爲字符串

首先,我是simulating the windows service in my ASP web application發送每週的電子郵件,所以在Global.asax我運行我的函數,將發送電子郵件。

現在電子郵件的內容是HTML,我想呈現視圖來獲取內容。問題是,在我的功能,我沒有任何以下內容:

  • Controller
  • ControllerContext
  • HttpContext
  • RoutData
  • ... &得多。這是有道理的,因爲函數被調用爲回調而不是HTTP請求操作。

我試圖使用RazorEngine通過讀取文件,然後使用Razor.Parse()方法使用部分作爲模板。但是我從這種方法面臨很多問題,因爲模板中沒有包含任何內容。我的意思是:它一直告訴我即使我包含System.Web.Mvc.Html,名稱「Html」在當前上下文中不存在'CompiledRazorTemplates.Dynamic.becdccabecff' does not contain a definition for 'Html'

我該如何解決這個問題?

+0

剃刀引擎需要的HttpContext,viewcontext解析視圖文件。您可以將帶有標記的HTML電子郵件模板放在單獨的文件中,然後在預定的任務中使用它來準備最終的電子郵件。 – eka 2013-05-05 07:19:45

回答

1

我認爲最好的方法是假設你開發了一個真正的NT服務,並使用HttpClient發送一個http請求到你的局部視圖,並以字符串的形式接收響應並用它來組成你的電子郵件。但是,通過在Scheduler類中進行一些更改,您可以在RunScheduledTasks方法中使用HttpContext

public delegate void Callback(); 

public delegate void Callback(HttpContext httpContext); 

添加cache.Current_HttpContext = HttpContext.Current;到運行方法

public static void Run(string name, int minutes, Callback callbackMethod) 
    { 
     _numberOfMinutes = minutes; 

     CacheItem cache = new CacheItem(); 
     cache.Name = name; 
     cache.Callback = callbackMethod; 
     cache.Cache = HttpRuntime.Cache; 
     cache.LastRun = DateTime.Now; 
     cache.Current_HttpContext = HttpContext.Current; 
     AddCacheObject(cache); 
    } 

變化CacheCallback

private static void CacheCallback(string key, object value, CacheItemRemovedReason reason) 
    { 
     CacheItem obj_cache = (CacheItem)value; 
     if (obj_cache.LastRun < DateTime.Now) 
     { 
      if (obj_cache.Callback != null) 
      { 
       obj_cache.Callback.Invoke(obj_cache.Current_HttpContext); 
      } 
      obj_cache.LastRun = DateTime.Now; 
     } 
     AddCacheObject(obj_cache); 
    } 

編輯: 如何使用HttpClient

HttpClient client = new HttpClient(); 
    HttpResponseMessage response = await client.GetAsync("http://localhost/controller/action/"); 
    response.EnsureSuccessStatusCode(); 
    string responseBody = await response.Content.ReadAsStringAsync(); 
+0

先生,我不認爲Global.asax應用程序中的'HttpContext不爲空'開始...對嗎? – Hilmi 2013-05-05 07:55:39

+0

是的,你說得對,它不是null,但是在你的回調方法中它是null,因爲從System.Web.Caching調用回調方法。緩存線程 – 2013-05-05 08:14:10

+0

在Global.asax的'Application_Start'方法中,HttpContext不爲null。但是在你的回調方法中它是空的,因爲從System.Web.Caching.Cache線程調用回調方法。 – 2013-05-05 10:31:49

0

不使用第三方庫,可以使用此方法生成的視圖中的Global.asax.cs字符串文件

public class EmptyController : Controller { } 

public string GetView(string viewName) 
    { 
    //Create an instance of empty controller 
    Controller controller = new EmptyController(); 

    //Create an instance of Controller Context 
    var ControllerContext = new ControllerContext(Request.RequestContext, controller); 

    //Create a string writer 
    using (var sw = new StringWriter()) 
    { 
     //get the master layout 
     var master = Request.IsAuthenticated ? "_InternalLayout" : "_ExternalLayout"; 

     //Get the view result using context controller, partial view and the master layout 
     var viewResult = ViewEngines.Engines.FindView(ControllerContext, viewName, master); 

     //Crete the view context using the controller context, viewResult's view, string writer and ViewData and TempData 
     var viewContext = new ViewContext(ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw); 

     //Render the view in the string writer 
     viewResult.View.Render(viewContext, sw); 

     //release the view 
     viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View); 

     //return the view stored in string writer as string 
     return sw.GetStringBuilder().ToString(); 
    } 
}