问题描述
我有工作 ASP.NET MVC 4 项目.我想向这个 MVC 项目添加另一个 WebForms 项目的 2 .aspx
页.我有几个问题:
I have working ASP.NET MVC 4 project. I want to add to this MVC project 2 .aspx
pages of another WebForms project. I have several questions:
- 我应该在哪里复制这个
.aspx
文件以及我应该如何配置我的路线? - 我应该如何告诉这个
.aspx
页面使用~/Shared/_Layout.chtml
作为母版页? - 此外,这个.aspx 页面使用了.ascx 控件.我应该把它们存放在哪里?
- 我应该如何修改 web.config 文件?
- Where should I copy this
.aspx
files and how should I config my routes? - How should I tell this
.aspx
pages to use~/Shared/_Layout.chtml
as a master page? - Additionally this .aspx pages uses .ascx controls. Where should I store them?
- How should I modify web.config file?
我查看了链接2203411/combine-asp-net-mvc-with-webforms">this 问题,但它们似乎已经过时.许多链接解释了如何将 MVC 添加到 WebForms,但我一直在寻找.
I have looked at the links posted in this question, but they seemed to be outdated. A lot of the links explain how to add MVC to WebForms, but I am looking for all the way around.
任何有用的链接将不胜感激.谢谢!
Any useful links would be appreciated. Thanks!
推荐答案
解决方案:
事实证明,将 .aspx 页面添加到现有的 MVC 项目比将 mvc 添加到 .aspx 更容易完成.对我来说最有趣的事情是发现 webforms 和 MVC,在一个项目的范围内共享 一个 IIS 上的运行时.
As it turned out, adding .aspx pages to the existing MVC project is an even easier task to accomplish than adding mvc to .aspx. The most interesting thing for me was to find out that both webforms and MVC, in the scope of one project share one runtime on IIS.
我做了什么:
- 将另一个项目的 .aspx 页面添加到我的 MVC 项目的根
- 为我的网络表单页面创建了新的母版页(右键单击项目->添加->新项目->母版页)
- 为了让 WF 的 Master Page 和 MVC 的 _Layout.chtml 共享相同的 Master 'View' 我发现 这篇很棒的文章,它允许您调用类似于.aspx 页面中的 >@Html.RenderPartial() 方法
以下代码提供了如何在 WebForms 中实现 RenderPartial 方法的信息:
- Added .aspx pages from another project to the root of my MVC project
- Created new master page for my webforms pages(right click project->add->new item->Master Page)
- In order to make Master Page of WF and _Layout.chtml of MVC share the same Master 'View' I found this great article that allows you to call something similar to @Html.RenderPartial() method within .aspx page
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 );
}
}
将其添加到 .aspx 页面的 codebehind.cs.然后你可以像这样从网络表单调用它:
Add it to codebehind.cs of your .aspx page. Then you can call it from the webforms like this:
<% 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.
That is all there is to it. As a result my _Layout.chtml and MasterPage.Master use the same shared partial views. And I can acess .aspx pages simply by navigating on them. If you have some issues with routing system, you can add routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
to your routeConfig in App_Start.
我使用的来源:
- 结合 asp net mvc 和 webforms
- 混合 Web 表单和 ASP.NET MVC
- MixingRazorViewsAndWebFormsMasterPages
- 如何在网络表单中包含部分视图一个>
我希望它会在以后对某人有所帮助.
I hope it will help somebody later on.
这篇关于如何将 .aspx 页面添加到现有的 MVC 4 项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!