2

I want to start IIS Express from within C# (to run UI Tests)

I have an MVC website that lives in "D:\GitHub\myProj\net5.Proj.Ui" - if I debug it within Visual Studio, the website runs perfectly well. Now I want to do some UI testing with selenium

My code starts IIS Express

var applicationPath = @"D:\GitHub\myProj\net5.Proj.Ui\bin\Debug\net6.0\";            
var programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
var _iisProcess = new Process();
_iisProcess.StartInfo.FileName = programFiles + @"\IIS Express\iisexpress.exe";
_iisProcess.StartInfo.Arguments = $"/path:{ applicationPath} /port:5000";
_iisProcess.EnableRaisingEvents = true;
_iisProcess.Start();
_iisProcess.WaitForExit();

The output gives me

Copied template config file 'C:\Program Files\IIS Express\AppServer\applicationhost.config' to 'C:\Users\Dave\AppData\Local\Temp\iisexpress\applicationhost2022715206710.config'
Updated configuration file 'C:\Users\Dave\AppData\Local\Temp\iisexpress\applicationhost2022715206710.config' with given cmd line info.
Starting IIS Express ...
Successfully registered URL "http://localhost:5000/" for site "Development Web Site" application "/"
Registration completed
IIS Express is running.
Enter 'Q' to stop IIS Express
Request started: "GET" http://localhost:5000/
Response sent: http://localhost:5000/ with HTTP status 403.14

When I open the browser, and nagivate to http://localhost:5000/ I get a 403 error, and I'm sure why

MyDaftQuestions
  • 4,487
  • 17
  • 63
  • 120
  • What are you exposing on `http://localhost:5000/`? Is it an API or MVC Application of what? – Paul Sinnema Jul 15 '22 at 20:29
  • @PaulSinnema a fairly standard MVC.NET website (built on .net 6) – MyDaftQuestions Jul 15 '22 at 20:33
  • Then you should be able to call a Controller. Did you add the correct routes? – Paul Sinnema Jul 15 '22 at 20:36
  • If I debug the website from within Visual Studio, it works perfectly... it's only an issue when I try to call this from command line – MyDaftQuestions Jul 15 '22 at 20:38
  • Tell me what you try to achieve? Why from the commandline? What result are you expecting? – Paul Sinnema Jul 15 '22 at 21:02
  • A glance on the path you used indicates that you don't fully understand how Visual Studio launches your ASP.NET Core web app for debugging, https://halfblood.pro/how-visual-studio-launches-iis-express-to-debug-asp-net-core-apps-d7fd3677e3c3 To do the same it requires a lot more than just running IIS Express from command line. – Lex Li Jul 16 '22 at 01:17
  • search and explore the error. – Vijunav Vastivch Jul 16 '22 at 01:28
  • My end goal is testing with selenium. I want to launch IIS on a given port and using Selenium, interface with the browser over localhost @PaulSinnema `I want to start IIS Express from within C# (to run UI Tests)` – MyDaftQuestions Jul 18 '22 at 16:26
  • @MyDaftQuestions: Add some code of the controller method you would like to call please. – Paul Sinnema Jul 18 '22 at 18:45
  • @PaulSinnema first off, thank you for staying with me on this question. My HomeController api is essentially empty, returning the view. The fact the website debugs fine via Visual Studio makes me think I'm missing an important point to your question. The other thing that strikes me is, testing with Selenium and MVC should be a solved problem. So why am I struggling – MyDaftQuestions Jul 18 '22 at 20:36
  • I've realised, the question I want to ask is "how do I test localhost with selenium" but... if I asked that, I would be penalised on SO for not trying.. As such, I've done research, I've made effort but I do worry it's turned to an XY problem – MyDaftQuestions Jul 18 '22 at 20:59
  • 1
    If your end goal is like that, then you shouldn't point to `D:\GitHub\myProj\net5.Proj.Ui\bin\Debug\net6.0`. Execute `dotnet publish` to generate the actual binaries for deployment, and then map your IIS Express site to this folder. You should also use a custom `applicationHost.config`, not `/path` and `/port`. – Lex Li Jul 18 '22 at 21:28
  • @LexLi, also, thank you for staying with me. Can you move your last comment to an answer please? – MyDaftQuestions Jul 19 '22 at 08:37

0 Answers0