Asp.net 如何将.aspx页面添加到现有的MVC4项目中?
我正在从事ASP.NETMVC4项目。我想在这个MVC项目中添加另一个WebForms项目的2Asp.net 如何将.aspx页面添加到现有的MVC4项目中?,asp.net,asp.net-mvc,asp.net-mvc-4,razor,webforms,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Razor,Webforms,我正在从事ASP.NETMVC4项目。我想在这个MVC项目中添加另一个WebForms项目的2.aspx页面。我有几个问题: 我应该在哪里复制此.aspx文件,以及如何配置我的 路线 如何告诉这个.aspx页面使用~/Shared/\u Layout.chtml作为母版页 此外,此.aspx页面使用.ascx控件。我应该把它们存放在哪里 我应该如何修改web.config文件 我看过有问题的帖子,但它们似乎已经过时了。很多链接解释了如何将MVC添加到WebForms中,但我一直在寻找 任何有用的
.aspx
页面。我有几个问题:
.aspx
文件,以及如何配置我的
路线李>
.aspx
页面使用~/Shared/\u Layout.chtml
作为母版页public class WebFormController : Controller { }
public static class WebFormMVCUtil
{
public static void RenderPartial( string partialName, object model )
{
//get a wrapper for the legacy WebForm context
var httpCtx = new HttpContextWrapper( System.Web.HttpContext.Current );
//create a mock route that points to the empty controller
var rt = new RouteData();
rt.Values.Add( "controller", "WebFormController" );
//create a controller context for the route and http context
var ctx = new ControllerContext(
new RequestContext( httpCtx, rt ), new WebFormController() );
//find the partial view using the viewengine
var view = ViewEngines.Engines.FindPartialView( ctx, partialName ).View;
//create a view context and assign the model
var vctx = new ViewContext( ctx, view,
new ViewDataDictionary { Model = model },
new TempDataDictionary() );
//render the partial view
view.Render( vctx, System.Web.HttpContext.Current.Response.Output );
}
}
将其添加到.aspx页面的codebehind.cs。然后您可以从webforms调用它,如下所示:
<% WebFormMVCUtil.RenderPartial( "ViewName", this.GetModel() ); %>
<% WebFormMVCUtil.RenderPartial("_Menu", null ); %>
<% WebFormMVCUtil.RenderPartial( "ViewName", this.GetModel() ); %>
<% WebFormMVCUtil.RenderPartial("_Menu", null ); %>
这就是全部。因此,我的\u Layout.chtml和母版页.Master使用相同的共享局部视图。我只需在.aspx页面上导航,就可以访问这些页面。如果您对路由系统有一些问题,可以添加routes.IgnoreRoute(“{resource}.aspx/{*pathInfo}”)代码>到应用程序启动中的routeConfig
我使用的资料来源:
我希望这会对以后的人有所帮助。Scott Hanselman的帖子仍然有效,无论您是将WebForm应用程序集成到MVC中,还是将其集成到MVC中,步骤都是一样的。将.aspx页面的Razor模板用作母版页不起作用,由于使用的视图引擎不同,因此无法将它们组合起来。@Andreas感谢您的评论。因此,如果无法将它们组合起来,是否可以使用另一种方法进行组合?最简单的方法是使用ASP.NET WebFormViewEngine(.aspx)母版页和Razor模板基础布局(.chtml),我在webform的调用中被困在GetModel()上。我看到的错误是“z.aspx不包含“GetModel”的定义,并且找不到接受“z_aspx”类型的第一个参数的扩展方法(是否缺少using指令或程序集引用?),我找不到定义GetModel的位置。