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

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

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

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


这是我使用的工作原理:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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...

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

1
C:\Windows\Microsoft.NET\Framework\v2.0.50727\Webdev.WebServer.exe /port:[PORT NUMBER] /path: [PATH TO ROOT]

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

自然地,您将需要将该版本号调整为最新/所需的版本号。


基于@Ray Vega的有用答案以及@James McLachlan对VS2010的重要更新,这是我的实现,涵盖了VS2012,并在必要时回退到VS2010。我还选择不只在Environment.Is64BitOperatingSystem上选择,因为它在我的系统上出错了。也就是说,我有一个64位系统,但是Web服务器位于32位文件夹中。因此,我的代码将首先查找64位文件夹,并在必要时退回到32位文件夹。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public void LaunchWebServer(string appWebDir)
{
    var PortNumber ="1162"; // arbitrary unused port #
    var LocalHostUrl = string.Format("http://localhost:{0}", PortNumber);
    var VirtualPath ="/";

    var exePath = FindLatestWebServer();

    var process = new Process
    {
        StartInfo =
        {
            FileName = exePath,
            Arguments = string.Format(
               "/port:{0} /nodirlist /path:"{1}" /virtual:"{2}"",
                PortNumber, appWebDir, VirtualPath),
            CreateNoWindow = true,
            UseShellExecute = false
        }
    };
    process.Start();
}

private string FindLatestWebServer()
{
    var exeCandidates = new List<string>
    {
        BuildCandidatePaths(11, true), // vs2012
        BuildCandidatePaths(11, false),
        BuildCandidatePaths(10, true), // vs2010
        BuildCandidatePaths(10, false)
    };
    return exeCandidates.Where(f => File.Exists(f)).FirstOrDefault();
}

private string BuildCandidatePaths(int versionNumber, bool isX64)
{
    return Path.Combine(
        Environment.GetFolderPath(isX64
            ? Environment.SpecialFolder.CommonProgramFiles
            : Environment.SpecialFolder.CommonProgramFilesX86),
        string.Format(
            @"microsoft shared\DevServer\{0}.0\WebDev.WebServer40.EXE",
            versionNumber));
}

我希望有见识的读者能够为VS2013提供合适的方法,因为它显然使用了不同的方案...


您可以轻松地使用Process Explorer查找手动启动它所需的完整命令行选项。
在调试网站时启动Process Explorer。对于VS2012,展开" devenv.exe"节点。右键单击" WebDev.WebServer20.exe",然后从中可以看到路径和命令行值。


推荐阅读

    linux服务器分区命令?

    linux服务器分区命令?,系统,管理,信息,设备,时间,命令,基础,首次,平均,情况,l

    vim运行linux命令?

    vim运行linux命令?,系统,工作,信息,地址,命令,标准,时间,情况,工具,基础,linu

    linux服务器上传命令?

    linux服务器上传命令?,服务,软件,平台,数据,工具,系统,手机,电脑,设备,官网,

    linux启动进命令行?

    linux启动进命令行?,系统,首页,密码,工具,终端,情况,电脑,数字,界面,命令,如

    linux启动蓝牙命令?

    linux启动蓝牙命令?,设备,手机,系统,蓝牙,电脑,管理,网络,密码,通讯,信息,Lin

    linux启动进去命令行?

    linux启动进去命令行?,系统,工具,首页,电脑,终端,材料,密码,命令,快捷键,窗

    linux下并行运行命令?

    linux下并行运行命令?,系统,服务,工作,命令,环境,网络,暂停,文件,脚本,参数,l

    linux启动显示命令行?

    linux启动显示命令行?,系统,密码,终端,状态,首页,情况,基础,电脑,信息,工具,l

    linux命令行启动登陆?

    linux命令行启动登陆?,密码,系统,电脑,地址,设计,代码,项目,连续,工具,庞大,L

    linux网络服务器命令?

    linux网络服务器命令?,地址,网络,系统,名称,信息,工具,状态,中心,灵活,基础,l

    linux的u盘启动命令?

    linux的u盘启动命令?,系统,电脑,工具,信息,软件,网上,名称,工作,设备,通用,从

    linux防火墙命令启动?

    linux防火墙命令启动?,系统,状态,管理,密码,服务,工具,防火墙,网络,信息,软

    linux下并行运行命令?

    linux下并行运行命令?,系统,服务,工作,命令,环境,网络,暂停,文件,脚本,参数,l

    linux启动显示命令行?

    linux启动显示命令行?,系统,密码,终端,状态,首页,情况,基础,电脑,信息,工具,l

    编程解析linux命令?

    编程解析linux命令?,系统,标准,基础,设备,发行,电脑,工具,密码,名字,适当,如

    linux服务器基本命令?

    linux服务器基本命令?,地址,系统,设备,网络,工作,标准,信息,电脑,命令,密码,l

    linux服务器下载命令?

    linux服务器下载命令?,服务,密码,系统,档案,工具,网络,公共,百度,地址,认证,l

    linux启动实例的命令?

    linux启动实例的命令?,密码,软件,环境,系统,管理,情况,工具,数据,设备,位置,l

    linux命令行后台运行?

    linux命令行后台运行?,服务,状态,标准,暂停,命令,后台,连续,地方,工作,方法,l

    linux退出启动命令行?

    linux退出启动命令行?,系统,状态,档案,平台,命令,环境,模式,终端,程序,编辑,l