解决方案:
事实证明,与将Mvc添加到.aspx相比,将.aspx页添加到现有的MVC项目是一件更容易完成的任务。对我而言,最有趣的事情是发现在一个项目范围内,webforms和MVC在IIS上共享一个运行时。
所以我做了什么:
以下代码提供了有关如何在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。然后,您可以从网络表单中调用它,如下所示:
<% WebFormMVCUtil.RenderPartial( "ViewName", this.GetModel() ); %>
由于我所有页面之间只有“菜单”共享,因此将其添加到部分视图中,然后在_Layout.chtml中将其命名
@Html.Partial("_Menu")
在MasterPage.Master中是这样的:
<% WebFormMVCUtil.RenderPartial("_Menu", null ); %>
这就是全部。结果,我的_Layout.chtml和MasterPage.Master使用相同的共享局部视图。我可以通过在.aspx页面上导航来访问它们。如果路由系统存在一些问题,可以routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
在App_Start中添加到routeConfig中。
我使用的来源:
希望以后能对您有所帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句