How to render an ASP.NET MVC view as a string?

后端 未结 15 2964
挽巷
挽巷 2020-11-21 04:40

I want to output two different views (one as a string that will be sent as an email), and the other the page displayed to a user.

Is this possible in ASP.NET MVC bet

15条回答
  •  再見小時候
    2020-11-21 05:02

    To repeat from a more unknown question, take a look at MvcIntegrationTestFramework.

    It makes saves you writing your own helpers to stream result and is proven to work well enough. I'd assume this would be in a test project and as a bonus you would have the other testing capabilities once you've got this setup. Main bother would probably be sorting out the dependency chain.

     private static readonly string mvcAppPath = 
         Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory 
         + "\\..\\..\\..\\MyMvcApplication");
     private readonly AppHost appHost = new AppHost(mvcAppPath);
    
        [Test]
        public void Root_Url_Renders_Index_View()
        {
            appHost.SimulateBrowsingSession(browsingSession => {
                RequestResult result = browsingSession.ProcessRequest("");
                Assert.IsTrue(result.ResponseText.Contains("

提交回复
热议问题