programatically changing the .net version of virtual directory

G

Guest

I am looking for a way to programatically change the .net version of the
virtual directory that I am creating within a aspx page.
As part of creating a new customer in my asp.net 2 application, it
automatically creates a virtual directory and configures it. I am using the
DirectoryServices.DirectoryEntry class in C# to do this. I am unable to set
the .Net version of the virtual directory from 1.1 to version 2. All searches
led me to the aspnet_regiis.exe. I tried 2 different solutions:-
----------------------------------------------------------------------
1. Call aspnet_regiis.exe -s W3SVC/1/root/vdirname from my aspx page after
creating the virtual directory. It fails because the process is running as
'NETWORK SERVICE' user who does not have enough permission to do these. When
I changed the identity of the Application Pool from 'Network Service' to
'Local System', I could run successfully change the version using
aspnet_regiis.exe

//Code in aspx page
string strFrameworkVersion =
ConfigurationManager.AppSettings["frameworkVersion"];
string winPath = Environment.GetEnvironmentVariable("windir");
string fullPath = winPath + @"\Microsoft.NET\Framework\v" +
strFrameworkVersion + @"\aspnet_regiis.exe";
string args = " -s " + "W3SVC/1/root/" + nameDirectory;

ProcessStartInfo startInfo = new ProcessStartInfo(fullPath);
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;

System.Diagnostics.Process process = new System.Diagnostics.Process();
startInfo.Arguments = args;
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();

---------------------------------------------------------------
2. I tried to modify the scriptmap programatically to match what
aspnet_regiis.exe does, i.e. changes the path to the right version of .NET.
But that made no difference. In fact, after running this code, the virtual
directory had no version of .NET selected.

PropertyValueCollection vals = siteVDir.Properties["ScriptMaps"];
ArrayList objScriptMaps = new ArrayList();
string frameworkVersion =
ConfigurationManager.AppSettings["frameworkVersion"];

foreach (string val in vals)
{
if (val.Contains("Framework"))
{
string version = val.Substring(val.IndexOf("Framework")
+ 10, 9);
if(version != frameworkVersion)
{
objScriptMaps.Add( val.Replace(version,
frameworkVersion));
}
}
else
{
objScriptMaps.Add(val);
}
}

siteVDir.Properties["ScriptMaps"].Value =
objScriptMaps.ToArray();
siteVDir.CommitChanges();
------------------------------------------

What is the most appropriate way to change the .NET version of the created
virtual directory, programatically from within as aspx page?

Thanks
Rakesh
 
G

Guest

You can check this site out:

http://www.denisbauer.com/NETTools/ASPNETVersionSwitcher.aspx

Rak said:
I am looking for a way to programatically change the .net version of the
virtual directory that I am creating within a aspx page.
As part of creating a new customer in my asp.net 2 application, it
automatically creates a virtual directory and configures it. I am using the
DirectoryServices.DirectoryEntry class in C# to do this. I am unable to set
the .Net version of the virtual directory from 1.1 to version 2. All searches
led me to the aspnet_regiis.exe. I tried 2 different solutions:-
----------------------------------------------------------------------
1. Call aspnet_regiis.exe -s W3SVC/1/root/vdirname from my aspx page after
creating the virtual directory. It fails because the process is running as
'NETWORK SERVICE' user who does not have enough permission to do these. When
I changed the identity of the Application Pool from 'Network Service' to
'Local System', I could run successfully change the version using
aspnet_regiis.exe

//Code in aspx page
string strFrameworkVersion =
ConfigurationManager.AppSettings["frameworkVersion"];
string winPath = Environment.GetEnvironmentVariable("windir");
string fullPath = winPath + @"\Microsoft.NET\Framework\v" +
strFrameworkVersion + @"\aspnet_regiis.exe";
string args = " -s " + "W3SVC/1/root/" + nameDirectory;

ProcessStartInfo startInfo = new ProcessStartInfo(fullPath);
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;

System.Diagnostics.Process process = new System.Diagnostics.Process();
startInfo.Arguments = args;
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();

---------------------------------------------------------------
2. I tried to modify the scriptmap programatically to match what
aspnet_regiis.exe does, i.e. changes the path to the right version of .NET.
But that made no difference. In fact, after running this code, the virtual
directory had no version of .NET selected.

PropertyValueCollection vals = siteVDir.Properties["ScriptMaps"];
ArrayList objScriptMaps = new ArrayList();
string frameworkVersion =
ConfigurationManager.AppSettings["frameworkVersion"];

foreach (string val in vals)
{
if (val.Contains("Framework"))
{
string version = val.Substring(val.IndexOf("Framework")
+ 10, 9);
if(version != frameworkVersion)
{
objScriptMaps.Add( val.Replace(version,
frameworkVersion));
}
}
else
{
objScriptMaps.Add(val);
}
}

siteVDir.Properties["ScriptMaps"].Value =
objScriptMaps.ToArray();
siteVDir.CommitChanges();
------------------------------------------

What is the most appropriate way to change the .NET version of the created
virtual directory, programatically from within as aspx page?

Thanks
Rakesh
 
G

Guest

Thanks for the response, but I need to do this programatically from withing
my aspx page. This tool internally uses aspnet_regiis, and I have explained
the problem of using it in my first post.


WoodenSWord said:
You can check this site out:

http://www.denisbauer.com/NETTools/ASPNETVersionSwitcher.aspx

Rak said:
I am looking for a way to programatically change the .net version of the
virtual directory that I am creating within a aspx page.
As part of creating a new customer in my asp.net 2 application, it
automatically creates a virtual directory and configures it. I am using the
DirectoryServices.DirectoryEntry class in C# to do this. I am unable to set
the .Net version of the virtual directory from 1.1 to version 2. All searches
led me to the aspnet_regiis.exe. I tried 2 different solutions:-
----------------------------------------------------------------------
1. Call aspnet_regiis.exe -s W3SVC/1/root/vdirname from my aspx page after
creating the virtual directory. It fails because the process is running as
'NETWORK SERVICE' user who does not have enough permission to do these. When
I changed the identity of the Application Pool from 'Network Service' to
'Local System', I could run successfully change the version using
aspnet_regiis.exe

//Code in aspx page
string strFrameworkVersion =
ConfigurationManager.AppSettings["frameworkVersion"];
string winPath = Environment.GetEnvironmentVariable("windir");
string fullPath = winPath + @"\Microsoft.NET\Framework\v" +
strFrameworkVersion + @"\aspnet_regiis.exe";
string args = " -s " + "W3SVC/1/root/" + nameDirectory;

ProcessStartInfo startInfo = new ProcessStartInfo(fullPath);
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;

System.Diagnostics.Process process = new System.Diagnostics.Process();
startInfo.Arguments = args;
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();

---------------------------------------------------------------
2. I tried to modify the scriptmap programatically to match what
aspnet_regiis.exe does, i.e. changes the path to the right version of .NET.
But that made no difference. In fact, after running this code, the virtual
directory had no version of .NET selected.

PropertyValueCollection vals = siteVDir.Properties["ScriptMaps"];
ArrayList objScriptMaps = new ArrayList();
string frameworkVersion =
ConfigurationManager.AppSettings["frameworkVersion"];

foreach (string val in vals)
{
if (val.Contains("Framework"))
{
string version = val.Substring(val.IndexOf("Framework")
+ 10, 9);
if(version != frameworkVersion)
{
objScriptMaps.Add( val.Replace(version,
frameworkVersion));
}
}
else
{
objScriptMaps.Add(val);
}
}

siteVDir.Properties["ScriptMaps"].Value =
objScriptMaps.ToArray();
siteVDir.CommitChanges();
------------------------------------------

What is the most appropriate way to change the .NET version of the created
virtual directory, programatically from within as aspx page?

Thanks
Rakesh
 
S

Steven Cheng[MSFT]

Hi Rak,

I think using external command utility to do the work is reasonable because
the ASP.NET version setting in IIS application vdir (or site) is a purely
IIS metabase setting. Also, perform such configuration will definitely
require powerful permission. And since you're going to execute the command
in ASP.NET application, I would suggest you consider using intergrated
windows authentication in IIS, and configure your ASP.NET application to
use windows authentication. And create an Admin-Page which let admin users
to visit. Thus, we can impersonate the client-user and spawn new process to
execute command under the impersonated user...

#How to implement impersonation in an ASP.NET application
http://support.microsoft.com/?id=306158

#How to spawn a process that runs under the context of the impersonated
user in Microsoft ASP.NET pages
http://support.microsoft.com/?id=889251

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

Thanks for the response Steve. Based on the responses I received, I gather
that aspnet_regiis is the only way to change the .NET version on IIS virtual
directory. It cannot be set using System.DirectoryServices.DirectoryEntry or
something similar.

Thanks
Rakesh
 
S

Steven Cheng[MSFT]

Hi Rakesh,

Actually, the ASP.NET version setting is the script extension mappings in
the IIS's site or virtual directory(application). You can check them in
the IIS manager, in the "Home Directory" Tab, click the "configuration"
button.......

And the aspnet_regiis.exe tool actually programmatically change the script
mappings internally(for all the extensions need to be processed by asp.net
, such as aspx, ascx , asax ......). Generally we won't consider manually
setting script mapping one by one, however, if necessary, you can still use
some scripts or .NET/ native code to set script mapping through ADSI
interface. Here is a web article which has mentioned this:

#Switching Versions of the ASP.Net Framework
http://aspalliance.com/306

Hope this also helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

Hi Steven,
Thanks for the response. The link you suggested in your response was doing
exactly what I tried as option 2. So I went back to my option 2 and
discovered a bug that was preventing the change in .Net Version. I was
deleting the character 'v' before the version number, and hence the directory
path became invalid.
So by changing the script map programatically, I can now successfully change
..NET version. And this method even works on remote IIS.
Thanks a lot for you help.

Rakesh
 
G

Guest

Just in case anyone is looking for the solution, here it is

PropertyValueCollection scriptMapVals = siteVDir.Properties["ScriptMaps"];
ArrayList objScriptMaps = new ArrayList();
string frameworkVersion =
ConfigurationManager.AppSettings["frameworkVersion"];
Regex versionRegex = new Regex(@"(?<=\\v)\d{1}\.\d{1}\.\d{1,5}(?=\\)");
//Assuming the version will always be something like n.n.nnnnn

foreach (string scriptMapVal in scriptMapVals)
{
if (scriptMapVal.Contains("Framework"))
{
objScriptMaps.Add(versionRegex.Replace(scriptMapVal, frameworkVersion));
}
else
{
objScriptMaps.Add(scriptMapVal);
}
}

siteVDir.Properties["ScriptMaps"].Value = objScriptMaps.ToArray();
siteVDir.CommitChanges();

Rakesh Chenchery
Symantec UK
 
S

Steven Cheng[MSFT]

Thanks for your followup Rakesh,

Glad to be of assistance and also thank you for the code sharing.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top