VSWebSite 接口

为网站项目提供属性和方法。

命名空间:  VsWebSite
程序集:  VsWebSite.Interop(在 VsWebSite.Interop.dll 中)

语法

声明
<GuidAttribute("70758CA4-91F8-46AD-80A7-73BC21BAE68B")> _
Public Interface VSWebSite
[GuidAttribute("70758CA4-91F8-46AD-80A7-73BC21BAE68B")]
public interface VSWebSite
[GuidAttribute(L"70758CA4-91F8-46AD-80A7-73BC21BAE68B")]
public interface class VSWebSite
[<GuidAttribute("70758CA4-91F8-46AD-80A7-73BC21BAE68B")>]
type VSWebSite =  interface end
public interface VSWebSite

VSWebSite 类型公开以下成员。

属性

  名称 说明
公共属性 CodeFolders 获取网站中被配置为代码文件夹的文件夹的集合。
公共属性 DTE 获取对包含此网站项目的 DTE2 对象的引用。
公共属性 Project 获取对此网站的引用作为 Project 对象。
公共属性 References 获取一个包含对当前网站的程序集和项目的引用的 AssemblyReferences 对象。
公共属性 TemplatePath 获取包含网站项模板的文件夹的完整路径和名称。
公共属性 URL 获取已用于打开此网站的 URL。
公共属性 UserTemplatePath 获取用于新项目项的用户模板文件夹的路径。
公共属性 VSWebSiteEvents 获取此网站的 VSWebSiteEvents 对象,此对象可用于添加事件处理程序。
公共属性 WebReferences 获取一个 WebReferences 对象,此对象包含对此网站使用的 Web 服务的引用。
公共属性 WebServices 获取一个 WebServices 对象,此对象包含一个由此网站公开的 Web 服务的集合。

页首

方法

  名称 说明
公共方法 AddFromTemplate 在此网站项目中创建新的 ProjectItem
公共方法 EnsureServerRunning 启动 ASP.NET 开发服务器(如有必要)并返回此网站的 URL。
公共方法 GetUniqueFilename 使用指定的根名称和文件扩展名返回在指定文件夹内唯一的文件名。
公共方法 PreCompileWeb 编译网站,并将输出写入指定的文件夹。
公共方法 Refresh 刷新显示以反映在 Visual Studio 之外对此网站所做的更改。
公共方法 WaitUntilReady 在后台进程执行完毕之前阻塞所有方法调用。

页首

备注

使用 VSWebSite 接口利用 Visual Studio 的宏或外接程序来操作网站项目。

除了此类中的属性和方法,使用 WebSiteProperties 类还可为网站项目提供其他属性。

提示

此类提供的功能在从 Visual Studio 2005 起的各个 Visual Studio 版本中均可用。 它在 Visual Web Developer 速成版中不可用。

示例

下面的示例演示如何使用外接程序与 Visual Studio 网站项目进行交互。 当将一个程序集的引用或一个 Web 服务的 Web 引用添加到此项目中时,此外接程序使用事件处理程序向事件日志写入内容。 此外,关闭此解决方案后,此外接程序将每个网站项目的摘要写入一个文本文件。

若要运行此示例,请使用如何:创建外接程序创建一个外接程序项目,然后使用此示例中的代码替换生成的 Connect.cs 文件中的所有代码。 您还将需要创建一个对 VsWebSite.Interop 程序集的引用。

[C#]

using System;
using System.IO;
using System.Collections;
using System.Diagnostics;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using VsWebSite;

// The object for implementing an Add-in.
public class Connect : IDTExtensibility2
{
    private DTE2 _applicationObject;
    private AddIn _addInInstance;

    // Implements the constructor for the Add-in object.
    // Created by the Add-In Wizard
    public Connect()
    {
    }

    // Event method created by the Add-In Wizard.
    // Occurs when the Add-In connects with the application.
    public void OnConnection(object application, ext_ConnectMode 
        connectMode, object addInInst, ref Array custom)
    {
        _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;

        // Attach Solution event handlers
        _applicationObject.DTE.Events.SolutionEvents.Opened 
            += new _dispSolutionEvents_OpenedEventHandler
                (SolutionEvents_Opened);
        _applicationObject.DTE.Events.SolutionEvents.QueryCloseSolution 
            += new _dispSolutionEvents_QueryCloseSolutionEventHandler
                (SolutionEvents_QueryCloseSolution);
    }

    // Custom event method that occurs before a solution is closed.
    private void SolutionEvents_QueryCloseSolution(ref bool fCancel)
    {
        foreach (Project proj in _applicationObject.Solution.Projects)
        {
            // Make sure background compilation is finished
            ((VSWebSite)proj.Object).WaitUntilReady();

            System.Text.StringBuilder strBldr = 
                new System.Text.StringBuilder();

            strBldr.AppendLine("Summary for Web Site: " 
                + ((VSWebSite)proj.Object).URL);
            strBldr.AppendLine("Solution: " 
                + _applicationObject.Solution.FullName);
            strBldr.AppendLine("Web References:");
            foreach (WebReference wref in 
                ((VSWebSite)proj.Object).WebReferences)
                strBldr.AppendLine("    " + wref.Namespace);
            strBldr.AppendLine("Assembly References:");
            foreach (AssemblyReference aref in 
                ((VSWebSite)proj.Object).References)
                strBldr.AppendLine("    " + aref.Name);
            // list the files?

            ((VSWebSite)proj.Object).VSWebSiteEvents.WebReferencesEvents.WebReferenceAdded
                -= WebRefEvents_WebRefAdded;

            string strBody = strBldr.ToString();

            // Save the summary as a text file in the Web site.
            string fName = _applicationObject.FullName;
            fName = fName.Substring(0, fName.Length - 4);
            fName += "." + ((VSWebSite)proj.Object).GetUniqueFilename
                ("/", "ProjectSummary", ".txt");
            if (File.Exists(fName))
                File.Delete(fName);
            StreamWriter sw = File.CreateText(fName);
            sw.Write(strBody);
            sw.Close();
        }
    }

    // Custom event method that occurs when a solution is opened.
    private void SolutionEvents_Opened()
    {
        // When solution is opened, attach event handlers for projects
        foreach (Project proj in _applicationObject.Solution.Projects)
        {   // Only attach event handlers if it is a Web site
            if (proj.Object is VSWebSite)
            {
                ((VSWebSite)proj.Object).VSWebSiteEvents.WebReferencesEvents.WebReferenceAdded +=
                    new _dispWebReferencesEvents_WebReferenceAddedEventHandler
                    (WebRefEvents_WebRefAdded);
                ((VSWebSite)proj.Object).VSWebSiteEvents.AssemblyReferencesEvents.AssemblyReferenceAdded += 
                    new _dispAssemblyReferencesEvents_AssemblyReferenceAddedEventHandler
                    (AssemblyRefsEvents_AssemblyRefAdded);
            }
        }
    }

    // Custom event method that occurs when a Reference is added.
    private void AssemblyRefsEvents_AssemblyRefAdded(AssemblyReference AssemblyRef)
    {
        EventLog appLog = new EventLog();
        appLog.Source = "VSWSTest." + AssemblyRef.ContainingProject.Name;
        appLog.WriteEntry("AssemblyReference added: " + AssemblyRef.Name);
    }
    
    // Custom event method that occurs when a Web Reference is added.
    private void WebRefEvents_WebRefAdded(WebReference webRef)
    {
        EventLog appLog = new EventLog();
        appLog.Source = "VSWSTest." + webRef.ContainingProject.Name;
        appLog.WriteEntry("WebReference added: " + webRef.Namespace);
    }

    #region Required but unused event handlers

    /// <summary>Implements the OnDisconnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being unloaded.</summary>
    /// <param term='disconnectMode'>Describes how the Add-in is being unloaded.</param>
    /// <param term='custom'>Array of parameters that are host application specific.</param>
    /// <seealso class='IDTExtensibility2' />
    public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
    {
    }

    /// <summary>Implements the OnAddInsUpdate method of the IDTExtensibility2 interface. Receives notification when the collection of Add-ins has changed.</summary>
    /// <param term='custom'>Array of parameters that are host application specific.</param>
    /// <seealso class='IDTExtensibility2' />
    public void OnAddInsUpdate(ref Array custom)
    {
    }

    /// <summary>Implements the OnStartupComplete method of the IDTExtensibility2 interface. Receives notification that the host application has completed loading.</summary>
    /// <param term='custom'>Array of parameters that are host application specific.</param>
    /// <seealso class='IDTExtensibility2' />
    public void OnStartupComplete(ref Array custom)
    {
    }

    /// <summary>Implements the OnBeginShutdown method of the IDTExtensibility2 interface. Receives notification that the host application is being unloaded.</summary>
    /// <param term='custom'>Array of parameters that are host application specific.</param>
    /// <seealso class='IDTExtensibility2' />
    public void OnBeginShutdown(ref Array custom)
    {
    }

    #endregion
}

请参见

参考

VsWebSite 命名空间

EnvDTE

WebSiteProperties

其他资源

自动化与扩展性参考

引用自动化程序集和 DTE2 对象

Visual Studio 宏

创建外接程序和向导