如何使用C#以编程方式运行ASP.Net开发服务器?

Ray*_*ega 11 c# asp.net

我有ASP.NET网页,我想为其构建自动化测试(使用WatiN和MBUnit).如何从我的代码启动ASP.Net开发服务器?我不想使用IIS.

Ray*_*ega 8

这就是我用过的方法:

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...
Run Code Online (Sandbox Code Playgroud)

  • 对于.NET 4,WebDev.WebServer40.exe位于程序文件(或程序文件(x86))\ Microsoft Shared\DevServer\10.0中.您可以使用Environment.Is64BitOperatingSystem来帮助确定要查找的文件夹.顺便说一句,您可以使用Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles)或Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFilesX86)获取文件夹. (3认同)

Dil*_*e-O 7

据我所知,您可以使用以下路径/语法从命令提示符启动dev服务器:

C:\Windows\Microsoft.NET\Framework\v2.0.50727\Webdev.WebServer.exe /port:[PORT NUMBER] /path: [PATH TO ROOT]
Run Code Online (Sandbox Code Playgroud)

...所以我可以想象你可以轻松地使用Process.Start()通过一些代码启动你需要的细节.

当然,您需要将该版本号调整为最近/期望的任何值.

  • 如果webdev.webserver.exe不在该文件夹中,则它可能位于C:\ Program Files\Common Files\Microsoft Shared\DevServer\9.0(我在那里找到它) (5认同)
  • 对于2013年,这里是http://stackoverflow.com/questions/4772092/starting-and-stopping-iis-express-programmatically (2认同)