解决方案:
事实证明,将.aspx页面添加到现有的MVC项目中比将mvc添加到.aspx更容易实现。最有趣的是,我发现在同一个项目的范围内,WebForms和MVC共享IIS上的一个运行时。
所以我做了什么:
The following code provides information how to implement RenderPartial method in WebForms:
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 );
}
}
Add it to codebehind.cs of your .aspx page. Then you can call it from the webforms like this:
<% WebFormMVCUtil.RenderPartial( "ViewName", this.GetModel() ); %>
Since I had only the 'menu' shared among all my pages, I added it to the partial View, then called it in _Layout.chtml
@Html.Partial("_Menu")
并且在 MasterPage.Master 中像这样:
<% WebFormMVCUtil.RenderPartial("_Menu", null ); %>
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
。我希望它以后能帮助某些人。