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:
我有工作的ASP。净MVC项目4。我想添加到另一个WebForms项目的MVC项目2 .aspx页面。我有几个问题:
- Where should I copy this
.aspx
files and how should I config my routes? - 我应该在哪里复制这个.aspx文件,我应该如何配置我的路由?
- How should I tell this
.aspx
pages to use~/Shared/_Layout.chtml
as a master page? - 如何告诉这个.aspx页面使用~/Shared/_Layout。chtml作为主页?
- Additionally this .aspx pages uses .ascx controls. Where should I store them?
- 此外,这个.aspx页面使用.ascx控件。我应该把它们放在哪里?
- How should I modify web.config file?
- 我该如何修改网络。配置文件?
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.
我看过这个问题的链接,但是它们似乎已经过时了。很多链接解释了如何将MVC添加到WebForms中,但是我一直在寻找。
Any useful links would be appreciated. Thanks!
如有任何有用的链接,我们将不胜感激。谢谢!
1 个解决方案
#1
16
The solution:
解决方案:
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.
事实证明,向现有MVC项目添加.aspx页面比向.aspx添加MVC更容易完成。对我来说最有趣的事情是发现在一个项目的范围内,webforms和MVC在IIS上共享一个运行时。
So what I did:
所以我所做的:
- Added .aspx pages from another project to the root of my MVC project
- 将另一个项目的.aspx页面添加到我的MVC项目的根中
- Created new master page for my webforms pages(right click project->add->new item->Master Page)
- 为我的webforms页面创建新的master页面(右键点击项目->添加->新项目-> master页面)
- 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
- 为了制作WF和_Layout的母版。MVC的chtml共享相同的主“视图”,我发现这篇很棒的文章允许您在.aspx页面中调用类似于@Html.RenderPartial()方法的东西
-
The following code provides information how to implement RenderPartial method in WebForms:
下面的代码提供了如何在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 ); } }
Add it to codebehind.cs of your .aspx page. Then you can call it from the webforms like this:
将它添加到后台代码。你的。aspx页面的cs。然后你可以从网络表单中这样调用它:
<% 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
由于我只有在所有页面*享的“菜单”,所以我将它添加到部分视图中,然后用_Layout.chtml调用它
@Html.Partial("_Menu")
and in MasterPage.Master like this:
MasterPage。主是这样的:
<% WebFormMVCUtil.RenderPartial("_Menu", null ); %>
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.
这就是全部。结果是我的_Layout。chtml MasterPage。Master使用相同的共享部分视图。我可以通过导航来访问。aspx页面。如果您有一些与路由系统有关的问题,您可以添加路由。ignoreroute(“{resource}.aspx/{*pathInfo}”);到App_Start的routeConfig。
Sources I used:
我使用来源:
- Combine asp net mvc with webforms
- 结合asp.net mvc和webforms
- Mixing Web Forms and ASP.NET MVC
- 混合Web表单和ASP。NET MVC
- MixingRazorViewsAndWebFormsMasterPages
- MixingRazorViewsAndWebFormsMasterPages
- How to include a partial view inside a webform
- 如何在webform中包含部分视图
I hope it will help somebody later on.
我希望这对以后的人有所帮助。
#1
16
The solution:
解决方案:
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.
事实证明,向现有MVC项目添加.aspx页面比向.aspx添加MVC更容易完成。对我来说最有趣的事情是发现在一个项目的范围内,webforms和MVC在IIS上共享一个运行时。
So what I did:
所以我所做的:
- Added .aspx pages from another project to the root of my MVC project
- 将另一个项目的.aspx页面添加到我的MVC项目的根中
- Created new master page for my webforms pages(right click project->add->new item->Master Page)
- 为我的webforms页面创建新的master页面(右键点击项目->添加->新项目-> master页面)
- 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
- 为了制作WF和_Layout的母版。MVC的chtml共享相同的主“视图”,我发现这篇很棒的文章允许您在.aspx页面中调用类似于@Html.RenderPartial()方法的东西
-
The following code provides information how to implement RenderPartial method in WebForms:
下面的代码提供了如何在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 ); } }
Add it to codebehind.cs of your .aspx page. Then you can call it from the webforms like this:
将它添加到后台代码。你的。aspx页面的cs。然后你可以从网络表单中这样调用它:
<% 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
由于我只有在所有页面*享的“菜单”,所以我将它添加到部分视图中,然后用_Layout.chtml调用它
@Html.Partial("_Menu")
and in MasterPage.Master like this:
MasterPage。主是这样的:
<% WebFormMVCUtil.RenderPartial("_Menu", null ); %>
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.
这就是全部。结果是我的_Layout。chtml MasterPage。Master使用相同的共享部分视图。我可以通过导航来访问。aspx页面。如果您有一些与路由系统有关的问题,您可以添加路由。ignoreroute(“{resource}.aspx/{*pathInfo}”);到App_Start的routeConfig。
Sources I used:
我使用来源:
- Combine asp net mvc with webforms
- 结合asp.net mvc和webforms
- Mixing Web Forms and ASP.NET MVC
- 混合Web表单和ASP。NET MVC
- MixingRazorViewsAndWebFormsMasterPages
- MixingRazorViewsAndWebFormsMasterPages
- How to include a partial view inside a webform
- 如何在webform中包含部分视图
I hope it will help somebody later on.
我希望这对以后的人有所帮助。