开发者

tool/script to visit all pages in an ASP.NET project?

开发者 https://www.devze.com 2023-03-30 21:18 出处:网络
Does anybody know of a tool, script, package, whatever that I can use to visit all pages in an ASP.NET Webforms web application project? (we aren\'t using any MVC functionality)

Does anybody know of a tool, script, package, whatever that I can use to visit all pages in an ASP.NET Webforms web application project? (we aren't using any MVC functionality)

Preferably, I would like to be able to generate a list of URLs to hit, edit the list so I can add some query string params, hit all the pages in the list, and collect HTTP response 开发者_如何学Gocodes: (200, 404, 500, 301, whatever).


  • Design time

Instead of using string literals for URLs in your application, define Url() methods in each page class like this:

public static string Url() { get { return "~/this_page.aspx"; } }
public static string Url(int ID) { get { return "~/this_page.aspx?id=" + ID; } }

Or list all URLs in a static class

public static class URL {
    public static string Login() { get { return "~/login.aspx"; } }
    public static string DisplayRecord(int recordID) 
        { get { return "~/display.aspx?id=" + recordID; } }
  • Runtime

Use a web testing framework to crawl all links and edit the result. I blogged about one possible solution using Selenium.


I made a WinForms application that gets the pages that can be accessed from the .csproject and can open them by clicking a button.

tool/script to visit all pages in an ASP.NET project?

using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;

namespace OpenAllPages
{
    public partial class Form1 : Form
    {
        public static IList<string> Pages;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string xmlstring = ReadXml("TaskManager.csproj");
            Pages = ParseAllPages(xmlstring);
            pagesListBox.DataSource = Pages;
        }

        private string ReadXml(string location)
        {
            try
            {
                var myFile = new StreamReader(location);
                string myString = myFile.ReadToEnd();
                myFile.Close();
                return myString;
            }
            catch (Exception e)
            {
                MessageBox.Show(String.Format("An error occurred: '{0}'", e.Message));
            }
            return null;
        }

        private IList<string> ParseAllPages(string xmlstring)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xmlstring);
            XPathNavigator nav = xmlDoc.DocumentElement.CreateNavigator();
            XmlNamespaceManager manager = new XmlNamespaceManager(nav.NameTable);
            manager.AddNamespace("x", "http://schemas.microsoft.com/developer/msbuild/2003");
            var elements = nav.Select("x:ItemGroup/x:Content", manager);

            var pageList = new List<string>();
            while (elements.MoveNext())
            {
                var page = elements.Current.GetAttribute("Include", "");
                if (page.EndsWith(".aspx"))
                    pageList.Add(page);
            }
            return pageList as IList<string>;
        }

        private string AddPagePrefix(string page)
        {
            return "http://localhost:8080/" + page;
        }

        private void openAllButton_Click(object sender, EventArgs e)
        {
            foreach (string page in Pages)
                System.Diagnostics.Process.Start("chrome.exe", AddPagePrefix(page));
        }
    }
}

Here is a link to the code

You need to place the project file which contains the pages you want to open in the OpenAllPages project and set it's Copy to Output property to "Copy if newer".

I Form1_Load change TaskManager.csproj to the name of your project file. And in:

System.Diagnostics.Process.Start("chrome.exe", AddPagePrefix(page));

rename parameter to the executable of the browser you are using.

0

精彩评论

暂无评论...
验证码 换一张
取 消