怎么在html添加aspx,asp.net – 如何添加.aspx页面到现有的MVC 4项目?

解决方案:

事实证明,将.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.

我使用的来源:

我希望以后能帮助别人.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值