How can I programmatically run the ASP.Net Development Server using C#?

前端 未结 4 2003
傲寒
傲寒 2021-02-04 11:41

I have ASP.NET web pages for which I want to build automated tests (using WatiN & MBUnit). How do I start the ASP.Net Development Server from my code? I do not want to use I

4条回答
  •  独厮守ぢ
    2021-02-04 12:14

    This is what I used that worked:

    using System;
    using System.Diagnostics;
    using System.Web;
    ...
    
    // settings
    string PortNumber = "1162"; // arbitrary unused port #
    string LocalHostUrl = string.Format("http://localhost:{0}", PortNumber);
    string PhysicalPath = Environment.CurrentDirectory //  the path of compiled web app
    string VirtualPath = "";
    string RootUrl = LocalHostUrl + VirtualPath;                 
    
    // create a new process to start the ASP.NET Development Server
    Process process = new Process();
    
    /// configure the web server
    process.StartInfo.FileName = HttpRuntime.ClrInstallDirectory + "WebDev.WebServer.exe";
    process.StartInfo.Arguments = string.Format("/port:{0} /path:\"{1}\" /virtual:\"{2}\"", PortNumber, PhysicalPath, VirtualPath);
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.UseShellExecute = false;
    
    // start the web server
    process.Start();
    
    // rest of code...
    

提交回复
热议问题