解决方案:
事实证明,将.aspx页面添加到现有的MVC项目中,比将.vx添加到.aspx更容易完成任务.对我来说最有趣的是找出在一个项目的范围内,Webforms和MVC都在IIS上共享一个运行时.
所以我做了什么
将另一个项目的.aspx页面添加到我的MVC项目的根目录
>为我的Webforms页面创建新的主页面(右键单击project-> add-> new item-> Master页面)
>为了使MVC的Master页面和MVC的_Layout.chtml共享相同的Master’View’,我发现可以在.aspx页面中调用类似于@ Html.RenderPartial()方法的this great article.
>以下代码提供了如何在WebForms中实现RenderPartial方法的信息:
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这样调用它:
>由于我只在所有页面中共享了“菜单”,所以我将其添加到部分视图中,然后在_Layout.chtml中调用它
@Html.Partial("_Menu")
并在MasterPage.Master中这样:
这就是它的一切.因此,我的_Layout.chtml和MasterPage.Master使用相同的共享部分视图.我可以通过浏览它们来访问.aspx页面.如果路由系统有一些问题,可以添加route.IgnoreRoute(“{resource} .aspx / {* pathInfo}”);到App_Start中的routeConfig.
我使用的来源:
我希望以后能帮助别人.