3

I'm curious about options for passing arguments to an MVC app (web app, rest service etc). There seems to be no support for this from what I can find however...

If I modify an app's Application_Start method (in Globals.asax.cs) and add a statement like this:

Environment.GetCommandLineArgs()

then run this under the Visual Studio debugger, I can see that fours args are being passed.

These are:

A path to the iisexpress.exe file.

A path to an applicationhost.config

A "/site" arg.

A "/apppool" arg.

This suggests that there actually is a way to pass arguments but nobody talks about it and its not documented. I'd like to add another custom arg here that our own code can look for and respond to if it's present.

1
  • 1
    Probably these are command line arguments for the IISExpress not for your application Commented May 22, 2017 at 19:32

2 Answers 2

1

An ASP.NET web site is not a Windows application. It does not have a .exe, for example. Instead, an ASP.NET web site contains a series of classes that conform to a particular interface that are pulled into the IIS process as plugins, in particular a Global.asax that inherits from HttpApplication. When an HTTP request comes in, IIS instantiates the HttpApplication and goes from there; this is not like running a program from the command line.

If you try to learn bits about the application process (e.g. command line arguments) via the various APIs, it is likely you will get information back not about your web site but about the IIS service process as a whole.

It is not possible to provide custom command line arguments to an ASP.NET web site. I would suggest you look into using Environment variables, registry entries, or web.config items instead.

Sign up to request clarification or add additional context in comments.

7 Comments

Yes, I can see this but want to some easy way to pass this to an app (outside of a configuration file) and in a debug session etc. It would have been very nice if Microsoft had designed ASP.NET so that command lines args could be provided (in debug and IIS configuration).
If you can tell me why a configuration file isn't suitable for your purposes I might be able to provide more guidance. Command line parameters would be a strange paradigm for an ASP.NET application because they start and recycle themselves all the time without any user intervention.
We use a greatly enhanced configuration system with several custom sections (by coincidence it is similar what's out now in .Net core). Our config file contains a single <environments> section, inside there are multiple <environment> sections each with a 'zone' attribute this string names the environment ('production', 'development' etc). Within each <environment> are the app settings and connection strings. There is also another section <deployment> mapped to a local file - this defines the zone etc. I'd like to avoid this and somehow pass the zone in.
In other words rather than needing to look inside the deployment section (loaded from a file) to see what zone is being used, I like to avoid this file altogether. The web.config is 100% generic and is deployed without alteration to any environment, which part of it is used is driven by the deployment.config file - however this can accidentally get copied/changed and I want to try and eliminate this if pos.
I would suggest that Zone should be part of the configuration of the machine and not deployed with the web site, so for example you could put it in Machine.config or ApplicationHost.config or you could add a registry key (e.g. HKLM\Software\Your Company\Zone). Another option is to look at the Host header in the HTTP request, which will tell you the fully-qualified domain name that the site is running on.
|
1

I'd like to simply expand @Steve's comment above.

A typical ASP.NET web app is always hosting by a process. That gives Environment.GetCommandLineArgs a possibility to return you some values as you discovered. However, it would be tricky to pass information via such, as in so many cases you don't have a meaningful way to pass the values you want.

IIS Express in Visual Studio

When you debug a project in VS, it usually runs on IIS Express, and VS does not provide you a place to configure which parameters to pass. (ASP.NET Core projects might be different.)

IIS on Windows Server

When your app finally deploys to a production server, it runs on IIS. IIS worker processes (w3wp.exe) are fully managed by Windows Process Activation Service, so again, you have no way to set which parameters to pass.

Self Hosting

Some project types (ASP.NET Web API/SignalR/WCF) allow you to do self hosting in a console process. You can then process command line parameters in that case.

However, in all, command line parameters are not part of Microsoft's design to pass information to ASP.NET apps. Don't go too far on this path.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.