Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在asp.net控件中使用asp.net mvc视图_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 在asp.net控件中使用asp.net mvc视图

C# 在asp.net控件中使用asp.net mvc视图,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,以下代码将asp.net webforms控件添加到webforms应用程序中的“我的内容占位符”: var someControl = (System.Web.UI.UserControl)LoadControl("~/serverPath/" + ControlName + ".ascx"); phContentControls.Controls.Add(someControl); 所以我想知道是否有任何方法可以将呈现的asp.net mvc视图添加到控件中 天哪,wtf我刚才问:你可以试

以下代码将asp.net webforms控件添加到webforms应用程序中的“我的内容占位符”:

var someControl = (System.Web.UI.UserControl)LoadControl("~/serverPath/" + ControlName + ".ascx");
phContentControls.Controls.Add(someControl);
所以我想知道是否有任何方法可以将呈现的asp.net mvc视图添加到控件中


天哪,wtf我刚才问:你可以试着做如下事情:

当我想在Webforms页面中呈现PartialView或ChildActions时,我会使用这个MvcUtility类,但我不认为我在UserControl中使用过它

不确定您使用的是哪个MVC版本,但我知道这适用于MVC 3和Razor视图

public static class MvcUtility
{
        public static void RenderPartial(string partialViewName, object model)
        {
            // Get the HttpContext
            HttpContextBase httpContextBase = new HttpContextWrapper(HttpContext.Current);
            // Build the route data, pointing to the Some controller
            RouteData routeData = new RouteData();
            routeData.Values.Add("controller", typeof(Controller).Name);
            // Create the controller context
            ControllerContext controllerContext = new ControllerContext(new RequestContext(httpContextBase, routeData), new Controller());
            // Find the partial view
            IView view = FindPartialView(controllerContext, partialViewName);
            // create the view context and pass in the model
            ViewContext viewContext = new ViewContext(controllerContext, view, new ViewDataDictionary { Model = model }, new TempDataDictionary(), httpContextBase.Response.Output);
            // finally, render the view
            view.Render(viewContext, httpContextBase.Response.Output);
        }

        private static IView FindPartialView(ControllerContext controllerContext, string partialViewName)
        {
            // try to find the partial view
            ViewEngineResult result = ViewEngines.Engines.FindPartialView(controllerContext, partialViewName);
            if (result.View != null)
            {
                return result.View;
            }
            // wasn't found - construct error message
            StringBuilder locationsText = new StringBuilder();
            foreach (string location in result.SearchedLocations)
            {
                locationsText.AppendLine();
                locationsText.Append(location);
            }
            throw new InvalidOperationException(String.Format("Partial view {0} not found. Locations Searched: {1}", partialViewName, locationsText));
        }

        public static void RenderAction(string controllerName, string actionName, object routeValues)
        {
            RenderPartial("RenderActionUtil", new RenderActionVM() { ControllerName = controllerName, ActionName = actionName, RouteValues = routeValues });
        }
    }
要渲染ChildAction,您需要共享MVC视图文件夹中的局部视图:

@model YourNamespace.RenderActionVM

@{
    Html.RenderAction(Model.ActionName, Model.ControllerName, Model.RouteValues);
}
以及视图模型:

public class RenderActionVM
{
    public string ControllerName { get; set; }
    public string ActionName { get; set; }
    public object RouteValues { get; set; }
}
最后,在您的webforms页面中,如下调用:

<% MvcUtility.RenderPartial("_SomePartial", null); %> 

<% MvcUtility.RenderAction("SomeController", "SomeAction", new { accountID = Request.QueryString["id"], dateTime = DateTime.Now }); %>

您可以尝试执行以下操作:

当我想在Webforms页面中呈现PartialView或ChildActions时,我会使用这个MvcUtility类,但我不认为我在UserControl中使用过它

不确定您使用的是哪个MVC版本,但我知道这适用于MVC 3和Razor视图

public static class MvcUtility
{
        public static void RenderPartial(string partialViewName, object model)
        {
            // Get the HttpContext
            HttpContextBase httpContextBase = new HttpContextWrapper(HttpContext.Current);
            // Build the route data, pointing to the Some controller
            RouteData routeData = new RouteData();
            routeData.Values.Add("controller", typeof(Controller).Name);
            // Create the controller context
            ControllerContext controllerContext = new ControllerContext(new RequestContext(httpContextBase, routeData), new Controller());
            // Find the partial view
            IView view = FindPartialView(controllerContext, partialViewName);
            // create the view context and pass in the model
            ViewContext viewContext = new ViewContext(controllerContext, view, new ViewDataDictionary { Model = model }, new TempDataDictionary(), httpContextBase.Response.Output);
            // finally, render the view
            view.Render(viewContext, httpContextBase.Response.Output);
        }

        private static IView FindPartialView(ControllerContext controllerContext, string partialViewName)
        {
            // try to find the partial view
            ViewEngineResult result = ViewEngines.Engines.FindPartialView(controllerContext, partialViewName);
            if (result.View != null)
            {
                return result.View;
            }
            // wasn't found - construct error message
            StringBuilder locationsText = new StringBuilder();
            foreach (string location in result.SearchedLocations)
            {
                locationsText.AppendLine();
                locationsText.Append(location);
            }
            throw new InvalidOperationException(String.Format("Partial view {0} not found. Locations Searched: {1}", partialViewName, locationsText));
        }

        public static void RenderAction(string controllerName, string actionName, object routeValues)
        {
            RenderPartial("RenderActionUtil", new RenderActionVM() { ControllerName = controllerName, ActionName = actionName, RouteValues = routeValues });
        }
    }
要渲染ChildAction,您需要共享MVC视图文件夹中的局部视图:

@model YourNamespace.RenderActionVM

@{
    Html.RenderAction(Model.ActionName, Model.ControllerName, Model.RouteValues);
}
以及视图模型:

public class RenderActionVM
{
    public string ControllerName { get; set; }
    public string ActionName { get; set; }
    public object RouteValues { get; set; }
}
最后,在您的webforms页面中,如下调用:

<% MvcUtility.RenderPartial("_SomePartial", null); %> 

<% MvcUtility.RenderAction("SomeController", "SomeAction", new { accountID = Request.QueryString["id"], dateTime = DateTime.Now }); %>