有没有办法来处理MVC视图(aspx文件)从非Web应用程序? [英] Is there a way to process an MVC view (aspx file) from a non-web application?

查看:242
本文介绍了有没有办法来处理MVC视图(aspx文件)从非Web应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个后台服务运行,它发送电子邮件给我的网站的用户。我想编写电子邮件模板为MVC的意见,以保持一致性(这样同一个模型可以用来发送一封电子邮件,以显示网页)。

I have a background service running which sends out emails to users of my website. I would like to write the email templates as MVC views, to keep things consistent (so that the same model can be used to send out an email as to display a web page).

不幸的是,当我尝试做一个LoadControl(它只是通过补丁来BuildManager.CreateInstanceFromVirtualPath),我得到以下内容:

Unfortunately, when I try to do a LoadControl (which simply patches through to BuildManager.CreateInstanceFromVirtualPath), I get the following:

System.NullReferenceException at
  System.Web.dll!System.Web.VirtualPath.GetCacheKey() + 0x26 bytes  
  System.Web.dll!System.Web.Compilation.BuildManager.GetCacheKeyFromVirtualPath + 0x2a bytes
  System.Web.dll!System.Web.Compilation.BuildManager.GetVPathBuildResultFromCacheInternal  + 0x30 bytes

看来,如果我MvcBuildViews设置为true,应该有使用编译的意见,建立一个电子邮件模板一些简单的方法,但我不能想出如何。

It seems that if I were to set MvcBuildViews to true, that there should be some easy way to use the compiled views to build an email template, but I can't figure out how.

我发现从里克施特拉尔以下的博客,这可能做的伎俩:
http://www.west-wind.com/$p$psentations /aspnetruntime/aspnetruntime.asp

I found the following blog from Rick Strahl, which may do the trick: http://www.west-wind.com/presentations/aspnetruntime/aspnetruntime.asp

不过,似乎开始了一个全新ASP.NET服务器处理的请求。

However, it seems to start up a whole ASP.NET server to process requests.

有一个简单的方式来加载一个MVC视图和放大器;渲染呢?或者是加载了ASP.NET运行时的唯一办法由Rick施特拉尔的建议?

Is there a simple way to load an MVC view & render it? Or is the only way to load up the ASP.NET runtime as suggested by Rick Strahl?

推荐答案

截至起来回答我的问题:)

Ended up answering my own question :)

public class AspHost : MarshalByRefObject
{
    public string _VirtualDir;
    public string _PhysicalDir;

    public string ViewToString<T>(string aspx, Dictionary<string, object> viewData, T model)
    {
        StringBuilder sb = new StringBuilder();
        using (StringWriter sw = new StringWriter(sb))
        {
            using (HtmlTextWriter tw = new HtmlTextWriter(sw))
            {
                var workerRequest = new SimpleWorkerRequest(aspx, "", tw);
                HttpContext.Current = new HttpContext(workerRequest);

                ViewDataDictionary<T> viewDataDictionary = new ViewDataDictionary<T>(model);
                foreach (KeyValuePair<string, object> pair in viewData)
                {
                    viewDataDictionary.Add(pair.Key, pair.Value);
                }

                object view = BuildManager.CreateInstanceFromVirtualPath(aspx, typeof(object));

                ViewPage viewPage = view as ViewPage;
                if (viewPage != null)
                {
                    viewPage.ViewData = viewDataDictionary;
                }
                else
                {
                    ViewUserControl viewUserControl = view as ViewUserControl;
                    if (viewUserControl != null)
                    {
                        viewPage = new ViewPage();
                        viewPage.Controls.Add(viewUserControl);
                    }
                }

                if (viewPage != null)
                {
                    HttpContext.Current.Server.Execute(viewPage, tw, true);

                    return sb.ToString();
                }

                throw new InvalidOperationException();
            }
        }
    }

    public static AspHost SetupFakeHttpContext(string physicalDir, string virtualDir)
    {
        return (AspHost)ApplicationHost.CreateApplicationHost(
            typeof(AspHost), virtualDir, physicalDir);
    }
}

然后,呈现一个文件:

Then, to render a file:

var host = AspHost.SetupFakeHttpContext("Path/To/Your/MvcApplication", "/");
var viewData = new ViewDataDictionary<SomeModelType>(){ Model = myModel };
String rendered = host.ViewToString("~/Views/MyView.aspx", new Dictionary<string, object>(viewData), viewData.Model);

这篇关于有没有办法来处理MVC视图(aspx文件)从非Web应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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