如何在我的项目之间共享母版页 [英] How to share Master Pages between my projects

查看:63
本文介绍了如何在我的项目之间共享母版页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Web项目,我需要共享一个母版页,以便在创建新页时具有基本布局.我想到的是使用该母版页创建一个项目,然后在每个Web项目中将其添加为参考.但是问题是我不知道如何将母版页嵌入到要应用MP的.aspx文件中,或者这是否是在项目之间共享母版页的最佳方法.我会很感激任何帮助或评论!

I'm working on a Web Project and I need to share one master page so that when I create a new page, it has the basic layout. What i've thought is to create a project with that master page and, in each web project, add it as a reference. But the problem is that i don't know how to embed the master page in the .aspx file that I want to apply the MP, or if it's the best way of sharing master pages between projects. I'll apreciate any help or comment!

在这里,我给你我的代码(这不起作用):

Here I give you my code (this doesn't work):

Index.aspx :

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="" CodeBehind="Index.aspx.cs" Inherits="MiWeb.Index" %>
    <HeaderMp:Header ID="ctntHead" ContentPlaceHolderID="head" runat="server">
        <title>SITPer</title>   
    </HeaderMp:Header>

Header.Master :

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Header.Master.cs" Inherits="MasterPages.Header" %>

<!DOCTYPE html>

<html>
<head>
    <title></title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <meta name="description" content="" />
        <meta name="keywords" content="" />
        <script src="js/jquery.min.js"></script>
        <script src="js/jquery.dropotron.min.js"></script>
        <script src="js/skel.min.js"></script>
        <script src="js/skel-layers.min.js"></script>
        <script src="js/init.js"></script>         
        <noscript>
            <link rel="stylesheet" href="css/skel.css" />
            <link rel="stylesheet" href="css/style.css" />
            <link rel="stylesheet" href="css/style-desktop.css" />
        </noscript>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <div>
        <!-- Header -->
        <div id="header-wrapper" >
            <div id="header">                   
                <!-- Logo -->
                <div id="logo">
                    <h1><a href="Index.aspx">SITPer</a></h1>
                    <p>Prueba</p>
                </div>
            </div>
        </div>  
        <asp:ContentPlaceHolder ID="body" runat="server">

        </asp:ContentPlaceHolder>
    </div>
</body>
</html>

谢谢!

推荐答案

注意::除了此处描述的链接文件功能外,还有一个新的 共享项目功能,您可以创建一个专为共享代码的项目,该代码将被编译到添加到该项目的项目中.

NOTE: In addition to the linked files feature described here, there is a new shared project feature in Visual Studio 2015+ that allows you to create a project exclusively for sharing code that will be compiled into the project it is added to.

母版页只能属于一个Web项目.但是,Visual Studio/MSBuild具有一项功能,可让您在项目之间共享代码以有效地获得所需的结果.如果将母版页文件(包括 .master .master.cs .master.designer.cs 的所有文件)放入一个文件夹与您的解决方案处于同一级别,您可以使用Visual Studio的链接文件功能将文件添加为链接文件(一个未复制到项目文件夹中的文件).

Master pages can only belong to a single web project. However, there is a feature of Visual Studio/MSBuild that allows you to share the code between projects to effectively get the desired result. If you put the master page files (all of them including .master, .master.cs and .master.designer.cs) into a folder at the same level as your solution you can use the linked file feature of Visual Studio to add the file as a linked file (one that is not copied into the project folder).

在Windows资源管理器中,打开包含解决方案( .sln )文件的目录.右键单击空白,然后单击 New->文件夹.将文件夹命名为 SharedFiles .将 Header.master Header.master.cs Header.master.designer.cs 文件复制到该文件夹​​中.

In Windows Explorer, open up the directory that has your solution (.sln) file. Right click in the whitespace and click New -> Folder. Name the folder SharedFiles. Copy the Header.master, Header.master.cs and Header.master.designer.cs files into the folder.

在Visual Studio中,从所有项目中删除 Header.master 文件.然后,对于您要共享 Header.master 页面的每个项目,请执行以下步骤.

In Visual Studio, delete the Header.master file(s) from all of the projects. Then follow these steps for each project you want to share the Header.master page in.

  1. 在解决方案资源管理器中右键单击要共享文件的项目,然后单击 Add->.现有项目.
  2. 在添加现有项"对话框中,导航到 SharedFiles 文件夹.
  3. 突出显示 Header.master Header.master.cs Header.master.designer.cs 文件.
  4. 在添加现有项"对话框的右下角,单击添加"按钮旁边的向下箭头(而不是添加"按钮本身!),然后单击下拉列表中的另存为链接"
  1. Right-click on the project you wish to share the file in Solution Explorer, and click Add -> Existing Item.
  2. In the Add Existing Item dialog, navigate to the SharedFiles folder.
  3. Highlight the Header.master, Header.master.cs and Header.master.designer.cs files.
  4. In the bottom right corner of the Add Existing Item dialog, click the down arrow next to the Add button (not the Add button itself!), and click the Add As Link item in the dropdown.

如果操作正确,文件图标上将带有箭头,指示它是链接文件.现在,当您对母版页进行更改时,更改将反映在所有项目中.

If you did it correctly, the icon of the file will have an arrow on it to indicate that it is a linked file. Now when you make changes to the master page, the changes will be reflected in all of the projects.

您还遇到了必须在Index.aspx页中解决的问题.您必须将母版页文件设置为文件的虚拟位置.如果您按照上述说明进行操作,则路径将为...

You also have an issue that you must fix in your Index.aspx page. You must set the master page file to the virtual location of the file. If you follow the instructions above, the path will be...

MasterPageFile="~/Header.master"

但是请注意,如果将文件(或链接的文件)放在Web项目的子目录中,则虚拟路径将会更改.

But note that the virtual path will change if you put the file (or the linked file) in a subdirectory of your web project.

这篇关于如何在我的项目之间共享母版页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆