13

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 IIS.

Ray
  • 187,153
  • 97
  • 222
  • 204

4 Answers4

8

From what I know, you can fire up the dev server from the command prompt with the following path/syntax:

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

...so I could imagine you could easily use Process.Start() to launch the particulars you need through some code.

Naturally you'll want to adjust that version number to whatever is most recent/desired for you.

Dillie-O
  • 29,277
  • 14
  • 101
  • 140
  • 5
    If webdev.webserver.exe is not located in that folder, it could be located in C:\Program Files\Common Files\Microsoft Shared\DevServer\9.0 (that's where I found it) – Casper Sep 22 '08 at 21:47
  • 2
    For 2013 here it is http://stackoverflow.com/questions/4772092/starting-and-stopping-iis-express-programmatically – Recursor Jul 03 '14 at 23:15
  • Maybe use relative paths ? _START /D %WEBDEV40% /B WebDev.WebServer40.exe /port:9195 /path:"..\..\Reale.VSIntegration\Reale.VSIntegration.SvcDespliegue.Host"_ not working for me – Kiquenet Sep 25 '14 at 07:10
8

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...
Ray
  • 187,153
  • 97
  • 222
  • 204
  • 3
    For .NET 4 WebDev.WebServer40.exe is in Program Files (or Program Files (x86))\Microsoft Shared\DevServer\10.0. You can use Environment.Is64BitOperatingSystem to help decide which folder to look in. BTW, you can get the folder(s) using Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles) or Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFilesX86). – James McLachlan Nov 08 '11 at 19:54
  • Another path: WebDev.WebServer40.exe in %ProgramFiles(x86)%\Common Files\Microsoft Shared\DevServer\11.0\ – Kiquenet Sep 25 '14 at 07:08
1

Building upon @Ray Vega's useful answer, and @James McLachlan's important update for VS2010, here is my implementation to cover VS2012 and fallback to VS2010 if necessary. I also chose not to select only on Environment.Is64BitOperatingSystem because it went awry on my system. That is, I have a 64-bit system but the web server was in the 32-bit folder. My code therefore looks first for the 64-bit folder and falls back to the 32-bit one if necessary.

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));
}

I am hoping that an informed reader might be able to supply the appropriate incantation for VS2013, as it apparently uses yet a different scheme...

Michael Sorens
  • 35,361
  • 26
  • 116
  • 172
0

You can easily use Process Explorer to find complete command line options needed for manually start it. Start Process Explorer while debugging your website. For VS2012, expand 'devenv.exe' node. Right-click on 'WebDev.WebServer20.exe' and from there you can see Path and Command Line values.

BHP
  • 443
  • 4
  • 13