English 中文(简体)
如何在 Razor 和 Webforms 之间共享主控主页
原标题:How to share master pages between Razor and Webforms

我之前问过这个问题,但问题是,我得到一个答复,这个答复不是针对我问的问题。 因此,要明确、直截了当地说,这个问题与什么无关:

<强 > 我不想在同一项目中使用九气、MVC和Webforms!

有人能给我提供一个链接吗? 一个项目是 ASP. Net 网络表格, 包含主页。 另一个项目是 MVC Razor 工程。 主页将提供连接.aspx 页面和.cshtml 视图的链接。 这些项目应该为 C#. 。 我需要下载源代码才能亲眼看到这一切是如何运作的 。

答案不是我要寻找的答案。我的问题非常具体。 <% 1 > /% 。

最佳回答

据我所知,这不可能,原因有两个:

  • You cannot use a .master page from outside the application. It must be part of the application (either as a file or as an embedded resource of a library combined with VirtualPathProvider trickery).
  • You cannot use a .master page from .cshtml, just like you can t use a .cshtml layout file from an .aspx page as a MasterPageFile.

所以直接重新使用您现有的. master 页面似乎是不可能的 。

问题回答

由于这些是两种不同的技术, 您将无法使用相同的 < em> file 。 您的 WebForms 应用程序需要有一个主页, 您的 MVC 应用程序需要有一个看起来相同的版式 。

如果你的主页/ Layout 没有动态内容, 它只是普通的 html, 我想你可以把它放在一个文件中, 创建一个脚本, 从它创建主页和布局文件, 但我不认为它值得。

我无法向您展示全部来源。 不是我的, 但我可以向您展示这个。 这是一个部分, 我们用 WebForm Master 页面来表达剃刀视图 。

- 草草景. 草...

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <% Html.RenderPartial((string) ViewBag._ViewName); %>
</asp:Content>
<asp:Content ID="scriptContent" ContentPlaceHolderID="ScriptContent" runat="server">

</asp:Content>

- - 使用 - - - 使用 - -

 public ActionResult Create(int clientId)
 {
....
return this.RazorView(choices); 

- 站点. 师傅(改编) - - - -

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>

<%@ Import Namespace="...." %>
<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
    <title>Some Alt</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />   
    <link href="../../Site.css" rel="stylesheet" type="text/css" />
    <asp:ContentPlaceHolder ID="ScriptContent" runat="server" />
    <meta http-equiv="Pragma" content="no-cache" />
    <meta http-equiv="Expires" content="-1" />
</head>
<body>
    <div class="page">
        <div id="main">
            <table width="100%">
                <tr>
                    <td valign="top" width="150" height="50">
                        <img src="<%= Common.CompanyLogoPath%>" alt="Some Alt width="150"
                            height="50" />
                    </td>
                    <td align="left" width="630" height="50" class="mainheading">
                        Description
                    </td>
                    <td align="right" valign="bottom">
                        <%= DateTime.Now.FormatShortDate() %>
                    </td>
                </tr>
                <tr>
                    <td colspan="3">
                        <hr />
                    </td>
                </tr>
                <tr>
                    <td valign="top" class="rightBorder">
                        <table>
                           .....
            </table>
        </div>
    </div>
</body>
</html>

Scott Hanselman和往常一样,对此有一些选择。你可以做到这一点,但并不漂亮:

http://www.hanselman.com/blog/MixingRazor Inview和WebForms MasterPages with ASPNETMVC3.aspx http://www.hanselman.com/blog/MixingRazor Inviews和Web Forms MasterPages with ASPNMVC3.aspx





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签