Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

asp.net

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.

like image 468
Ray Avatar asked Sep 11 '08 17:09

Ray


3 Answers

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...
like image 160
Ray Avatar answered Oct 27 '22 16:10

Ray


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.

like image 22
Dillie-O Avatar answered Oct 27 '22 16:10

Dillie-O


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

like image 1
Michael Sorens Avatar answered Oct 27 '22 15:10

Michael Sorens