ASP.NET MVC使用的控制器或MVC应用程序上下文之外查看 [英] ASP.NET MVC Use Controller or View outside of the MVC application context

查看:174
本文介绍了ASP.NET MVC使用的控制器或MVC应用程序上下文之外查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我创建一个小的网页,让我们的用户在我们的数据库中进行搜索的界面。

本网站必须提供2 web服务功能,搜索(字符串搜索字符串)和GetAccountInfoByInitials(字符串缩写)

我想用我的控制器,要做到这一点,但我不能找出如何我可以得到HTML出的ViewResult的。

我已经tryed以下,但result.ToString()只给我字符串System.Web.Mvc.ViewResult

 公共类SearchService:ISearchService{私人只读ServiceHandlerController _controller;
公共SearchService()
{
    _controller =新ServiceHandlerController();
}公共字符串搜索(字符串搜索字符串)
{
    VAR的结果= _controller.Search(搜索字符串);
    返回result.ToString();
}公共字符串GetAccountInfoByInitials(字符串缩写)
{
    VAR的结果= _controller.Search(英文缩写)的ToString();
    返回结果;
}}


解决方案

这是一个问题的答案我张贴类似于此:
<一href=\"http://stackoverflow.com/questions/3702526/is-there-a-way-to-process-an-mvc-view-aspx-file-from-a-non-web-application\">http://stackoverflow.com/questions/3702526/is-there-a-way-to-process-an-mvc-view-aspx-file-from-a-non-web-application

 公共类AspHost:MarshalByRefObject的
{
    公共字符串_VirtualDir;
    公共字符串_PhysicalDir;    公共字符串ViewToString&LT; T&GT;(串ASPX,字典&LT;字符串对象&gt;可视数据,T型)
    {
        StringBuilder的SB =新的StringBuilder();
        使用(StringWriter的SW =新的StringWriter(某人))
        {
            使用(HtmlTextWriter的TW =新的HtmlTextWriter(SW))
            {
                VAR workerRequest =新的SimpleWorkerRequest(ASPX,,TW);
                HttpContext.Current =新的HttpContext(workerRequest);                的ViewDataDictionary&LT; T&GT;的ViewDataDictionary =新的ViewDataDictionary&LT; T&GT;(模型);
                的foreach(KeyValuePair&LT;字符串对象&gt;对在可视数据)
                {
                    viewDataDictionary.Add(pair.Key,pair.Value);
                }                对象视图= BuildManager.CreateInstanceFromVirtualPath(ASPX的typeof(对象));                的ViewPage的ViewPage =视图的ViewPage;
                如果(的ViewPage!= NULL)
                {
                    viewPage.ViewData =的ViewDataDictionary;
                }
                其他
                {
                    ViewUserControl viewUserControl =视图ViewUserControl;
                    如果(viewUserControl!= NULL)
                    {
                        的ViewPage =新的ViewPage();
                        viewPage.Controls.Add(viewUserControl);
                    }
                }                如果(的ViewPage!= NULL)
                {
                    HttpContext.Current.Server.Execute(的ViewPage,TW,真);                    返回sb.ToString();
                }                抛出新的InvalidOperationException异常();
            }
        }
    }    公共静态AspHost SetupFakeHttpContext(字符串physicalDir,串virtualDir)
    {
        返回(AspHost)ApplicationHost.CreateApplicationHost(
            typeof运算(AspHost),virtualDir,physicalDir);
    }
}

然后,呈现一个文件:

  VAR主机= AspHost.SetupFakeHttpContext(路径/要/你/ MvcApplication,/);
VAR可视数据=新的ViewDataDictionary&LT; SomeModelType&GT;(){型号=基于myModel};
字符串呈现= host.ViewToString(〜/查看/ MyView.aspx,新的字典&LT;字符串对象&gt;(可视数据),viewData.Model);

Hello i am creating a small webpage that give our users an interface to search in our database.

This website must supply 2 webservice functions, Search(string searchString) and GetAccountInfoByInitials(string initials)

I would like to use my controllers to do this, but i can not find out how i can get the html out of a ViewResult.

I have tryed the following, but the result.ToString() only give me the string "System.Web.Mvc.ViewResult"

public class SearchService : ISearchService

{

private readonly ServiceHandlerController _controller;
public SearchService()
{
    _controller = new ServiceHandlerController();
}

public string Search(string searchString)
{
    var result = _controller.Search(searchString);
    return result.ToString();
}

public string GetAccountInfoByInitials(string initials)
{
    var result = _controller.Search(initials).ToString();
    return result;
}

}

解决方案

This is an answer to a question I posted similar to this one: http://stackoverflow.com/questions/3702526/is-there-a-way-to-process-an-mvc-view-aspx-file-from-a-non-web-application

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);

这篇关于ASP.NET MVC使用的控制器或MVC应用程序上下文之外查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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