Is there a way to process an MVC view (aspx file) from a non-web application?

后端 未结 5 1290
执念已碎
执念已碎 2020-12-14 23:55

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 tha

5条回答
  •  抹茶落季
    2020-12-15 00:55

    The default asp.net view engine is tied to the asp.net engine. Its tied to the context, I think you can work around it but its definitely not simple.

    The issue is with the default view engine + asp.net engine combination, other view engines shouldn't have that issue. At the very least the spark view engine doesn't.


    Edit: OP solved with the last hints, but fwiw my version that uses the controller home index action of the default asp.net mvc project template:

    public class MyAppHost : MarshalByRefObject
    {
        public string RenderHomeIndexAction()
        {
            var controller = new HomeController();
            using (var writer = new StringWriter())
            {
                var httpContext = new HttpContext(new HttpRequest("", "http://example.com", ""), new HttpResponse(writer));
                if (HttpContext.Current != null) throw new NotSupportedException("httpcontext was already set");
                HttpContext.Current = httpContext;
                var controllerName = controller.GetType().Name;
                var routeData = new RouteData();
                routeData.Values.Add("controller", controllerName.Remove(controllerName.LastIndexOf("Controller")));
                routeData.Values.Add("action", "index");
                var controllerContext = new ControllerContext(new HttpContextWrapper(httpContext), routeData, controller);
                var res = controller.Index();
                res.ExecuteResult(controllerContext);
                HttpContext.Current = null;
                return writer.ToString();
            }
        }
    }
    

    ... from a separate project:

        [TestMethod]
        public void TestIndexAction()
        {
            var myAppHost = (MyAppHost)ApplicationHost.CreateApplicationHost(
                typeof(MyAppHost), "/", @"c:\full\physical\path\to\the\mvc\project");
            var view = myAppHost.RenderHomeIndexAction();
            Assert.IsTrue(view.Contains("learn more about"));
    
        }
    

    Some extra notes:

    • url in new HttpRequest doesn't matter, but needs to be a valid url
    • it isn't meant to be used from an asp.net app that already has a context / that said, I'm not sure if it'd actually spawn the new AppDomain and work
    • Controller type's constructor and specific instance is explicit in the code, could be replaced with something to be passed in the parameters, but need to deal with the restrictions of MarshalByRef / worst case some simple reflection could be used for it

提交回复
热议问题