Run ASP.NET Web Server From Any Folder

Rob McLaws has this cool extension that lets you click on a folder and run a webserver from it.

The article, with some comments on alternative ways to do it, or ways to improve it can be found here.

http://weblogs.asp.net/rmclaws/archive/2005/10/25/428422.aspx

From the page:

I’ve been doing some web development work again lately, and I haven’t wanted to screw with setting up IIS on my virtual machine. The .NET Framework 2.0 comes with a built-in webserver, based on the old Cassini web server. So I wanted to be able to easily set up any directory to have its contents served up on an as-needed basis. The result is an addition to the Windows Explorer right-click menu, as shown below:

This is enabled by a simple modification to the registry. You can take the text below and dump it into a file named “WebServer.reg”, and then run it, to immediately get the item in the context menu.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINESOFTWAREClassesFoldershellVS2005 WebServer]
@=”ASP.NET 2.0 Web Server Here”

[HKEY_LOCAL_MACHINESOFTWAREClassesFoldershellVS2005 WebServercommand]
@=”C:\Windows\Microsoft.NET\Framework\v2.0.50727\Webdev.WebServer.exe /port:8080 /path:”%1″”

There are a couple caveats to this tool, however:

  1. The web server does not randomly assign a port number, so it has to be hard-coded into the registry.
  2. The web server does not automatically assign a virtual directory, so it will always be “http://localhost:port”
  3. A command prompt window will be spawned, and cannot be closed without closing the web server process.

Moving foward, there are a couple of options to fix these issues:

  1. Someone who knows more about variables in the registry can help me fix issues #2 and #3
  2. I can build a wrapper executable that solves all three problems
  3. Microsoft can put in a DCR and fix it before it goes gold in a few days.

#3 is not likely, so I’d love to hear what you think. Hope this trick is useful.

 

Dropdown1 has a SelectedValue which is invalid because it does not exist in the list of items.

‘Dropdown1’ has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

This error has been a real pain to deal with.  I finally have a work around and a guess at the underlying problem, and because I see that people are having this same problem I thought I would do a little writeup about it.

 

Obviously, if you look at the actual error message, there are cases when you can get this message and the problem is easy to solve.  e.g. you have a dropdown populated with EmploID values and you try to databind with: 

Value=’<%# Bind(“EmployeeName”) %>

 
Well of course that won’t work if all your values are integers and you are trying to set the value to “Bill Smith”.
 
But my problem was a bit more complicated.
 
I was building a web user control (aka ASCX) that was a “Location” dropdown.  The control basically contains a dropdown, and all the necessary logic to populate the dropdown, and I was trying to use this control inside the EditTemplate of a GridView.
 
To make things more complicated, in this case, I didn’t JUST want all the Locations, I also wanted to add a “Please Select” item to the list, with a value of 0.
 
But I kept getting the error above when the value was supposed to be 0.
 
I stepped through my code to try to find out what was going on.  I believe the issue is this:
 
On load, on value set, and on databind I call my “Populate” method.  This method sees if there are items in the dropdown.  If not, it gets a list from the database and binds to the dropdown.  It also checks if there is a “Please Select” item, and adds it if there isn’t one.
 
But still when Dropdown’s “DataBound” event was fired the last time, my manually added item had been removed.
 
So what I think what is happening is that :
1) I set the dropdown DataSource to a collection of business objects
2) I add the “Please Select” item
3) The dropdown is told to databind itself by the Gridview.
4) The dropdown says… “Oh, I do have a datasource, so I’m going to ditch my current list and rebind to that old collection”
5) The dropdown says “Holy shnikies, I am supposed to bind to value “0”, but I don’t have one!!”
 
I think there are 2 possible solutions.
 
1) I created an adapter class that only has Id and Description properties that are going to be used by the control, and I wrote a function to take my collection of business objects and build a collection of Adapters (adding my “Please Select”) to the list of adapters.  To see what I am talking about I will post this code below.
 
2) Manually add items to the dropdown instead of databinding, so that when the control is told to databind, it doesn’t have a datasource to use.
 
 
Here is my adapter code:
            '**** In my populate method
Me.LocationsDropdown.DataSource = LocationAdapter.LocationsToAdapters(locations)
Me.LocationsDropdown.DataTextField = "LocationDescriptionWithActiveAttribute"
Me.LocationsDropdown.DataValueField = "LocationId"
Me.LocationsDropdown.DataBind()



'*************************************
'*************************************




Private Class LocationAdapter
Public ciLocationId As Integer
Public csLocationDescriptionWithActiveAttribute As String
Public Property LocationId() As Integer
Get
Return ciLocationId
End Get
Set(ByVal value As Integer)
Me.ciLocationId = Value
End Set
End Property

Public Property LocationDescriptionWithActiveAttribute() As String
Get
Return csLocationDescriptionWithActiveAttribute
End Get
Set(ByVal value As String)
Me.csLocationDescriptionWithActiveAttribute = Value
End Set
End Property

Public Sub New(ByVal loc As Location)
Me.LocationId = loc.LocationId
Me.LocationDescriptionWithActiveAttribute = loc.LocationDescriptionWithActiveAttribute
End Sub
Public Sub New(ByVal Id As Integer, ByVal Desc As String)
Me.LocationId = Id
Me.LocationDescriptionWithActiveAttribute = Desc
End Sub

Public shared Function LocationsToAdapters(ByVal locations As List(Of Location)) As List(Of LocationAdapter)
Dim adapters As New List(Of LocationAdapter)
adapters.Add(New LocationAdapter(0, "--Location"))
For Each loc As Location In locations
adapters.Add(New LocationAdapter(loc))
Next
Return adapters
End Function

End Class
 
 
 

ASP.NET 2.0 Page Life-cycle

This table is from: http://www.csharper.net/blog/page_lifecycle_methods_in_asp_net_2_0.aspx

 

Method Postback Control
Constructor Always All
Construct Always Page
TestDeviceFilter Page
Used to determine which device filter is in place, and use this information to decide how to display the page.
AddParsedSubObject Always All
Notifies the server control that an element, either XML or HTML, was parsed, and adds the element to the server control’s ControlCollection object.
DeterminePostBackMode Always Page
Returns a NameValueCollection object that contains the data posted back to the page. The presence of the page hidden fields VIEWSTATE and EVENTTARGET is used to help determine whether a postback event has occurred. The IsPostBack property is set when the DeterminePostBackMode method is called.
OnPreInit Always Page
Called at the beginning of the page initialization stage. After the OnPreInit method is called, personalization information is loaded and the page theme, if any, is initialized. This is also the preferred stage to dynamically define a PageTheme or MasterPage for the Page.
OnInit Always All
Performs the initialization and setup steps required to create a Page instance. In this stage of the page’s life cycle, declared server controls on the page are initialized to their default state; however, the view state of each control is not yet populated. A control on the page cannot access other server controls on the page during the Page_Init phase, regardless of whether the other controls are child or parent controls. Other server controls are not guaranteed to be created and ready for access.
OnInitComplete Always Page
Called after page initialization is complete. In this stage of the page’s life cycle, all declared controls on the page are initialized, but the page’s view state is not yet populated. You can access server controls, but they will not yet contain information returned from the user.
LoadPageStateFromPersistenceMedium Postback Page
Uses the Load method of the System.Web.UI.PageStatePersister object referenced by the PageStatePersister property to load any saved view-state information for the Page object.
LoadControlState Postback All
Restores control-state information from a previous page request that was saved by the SaveControlState method.
LoadViewState Postback All
Restores view-state information from a previous page request that was saved by the SaveViewState method.
OnPreLoad Always Page
Called after all postback data returned from the user is loaded. At this stage in the page’s life cycle, view-state information and postback data for declared controls and controls created during the initialization stage are loaded into the page’s controls. Controls created in the OnPreLoad method will also be loaded with view-state and postback data.
OnLoad Always All
Notifies the server control that it should perform actions common to each HTTP request for the page it is associated with, such as setting up a database query. At this stage in the page lifecycle, server controls in the hierarchy are created and initialized, view state is restored, and form controls reflect client-side data.
RaisePostBackEvent Postback All
Notifies the server control that caused the postback that it should handle an incoming postback event.
OnLoadComplete Always Page
At this point in the page life cycle, all postback data and view-state data is loaded into controls on the page.
OnPreRender Always All
Notifies the server control to perform any necessary prerendering steps prior to saving view state and rendering content.
OnPreRenderComplete Always Page
At this stage of the page life cycle, all controls are created and the page is ready to render the output. This is the last event called before the page’s view state is saved.
SaveControlState Always All
Saves any server control state changes that have occurred since the time the page was posted back to the server. If there is no state associated with the control, this method returns a null reference. Custom controls using control state must call the RegisterRequiresControlState method on the Page before saving control state.
SaveViewState Always All
Saves any server control view-state changes that have occurred since the time the page was posted back to the server. If there is no view state associated with the control, this method returns a null reference.
SavePageStateToPersistenceMedium Always Page
Saves any view-state and control-state information for the page. The SavePageStateToPersistenceMedium method uses the Save method of the System.Web.UI.PageStatePersister object referenced by the PageStatePersister property to store view-state and control-state information for the page.
Render Always All
Initializes the HtmlTextWriter object and calls on the child controls of the Page to render. The Render method is responsible for creating the text and markup that is sent to the client browser. The default Render method calls RenderChildren to write the text and markup for the controls contained on the page.
OnUnload Always All
Used to do target-specific processing in the Unload stage of the control lifecycle. Typically, these are cleanup functions that precede disposition of the control.

Delete the Web Apps DLLs and Everything Works

Here is another of my favorite problems with VS2005.

If I have a Web Application Project.  If I build it (with no errors) then when I try to view it I get the error below.  However,

Parser Error

Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: The type ‘Company.Web.Equipment.Global’ is ambiguous: it could come from assembly ‘C:WINDOWSMicrosoft.NETFrameworkv2.0.50727Temporary ASP.NET Filesequipment3f386648c5a85b94App_Code.gv_z5p7w.DLL’ or from assembly ‘C:DataCompanyCompany.Web.EquipmentbinCompany.Web.Equipment.DLL’. Please specify the assembly explicitly in the type name.

Source Error:

Line 1:  <%@ Application Inherits="Company.Web.Equipment.Global" Language="VB" %>

Source File: /Equipment/global.asax    Line: 1


Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42

 

I don’t know why it is only happening with this one web app.

Very frustrating.

Update: This has been resolved.  The problem is obvious now.  This was a web application that had been converted to a Web Site Project.  When I tried to reimport the files manually into a new Web Application Project there was one minor, yet major, difference that was causing it to fail.  The “CodeBehind” attribute had been changed to “CodeFile”.  I didn’t think much of it at the time, but of course this indicates that it is going to actually USE the codefile when the page is accessed.  By having a CodeFile attribute AND compiling the code into a DLL, I was ending up with copies of every class.

Sending Datasets and Objects Over the Wire: Serialization and XML

I have tried to councel against sending datasets across web service calls, but we have a lot of instances where this is being done.

One of the problems with this is that datasets get bloated when converted to XML.

So I set out to compare the sizes of:

  1. Serialized List(Of MyType)
  2. Serialized DataTable
  3. Serialized DataSet
  4. XML Serialized Dataset

I wish I had done some research on this, because I would have quickly been reminded that DataSets always serialize as XML, even if you are using the BinaryFormatter. 

There are lots of people out there coming up with their own ideas for how to improve the serialization of datasets:

Anyway, this isn’t really THAT big of a deal, because my real goal wasn’t to improve the dataset serialization, but to simply see what it would be, and compare it to some other ways to serialize data, like in a list of business objects or a datatable.

The results are interesting:

Given a list of 1013 Business Objects (Records) the serialization results are as follows:

Method Size (bytes)
List(Of MyType) 290,321
DataTable 819,575
DataSet 693,088
XML Serialzied Dataset 851,614

I have read that you can really decrease the size of the dataset by writing your own logic to do the serialization, but as everyone points out that is kind of a pain.

Upgrading to 2005 Continuing To Be A Pain

I decided to go with Scott Gu’s suggestion to use Web Application Projects to help ease the conversion for 2003.

I will agree that this is a much better way to convert your project than to turn it into a Web Site Project, but there are still lingering problems.

In one of my projects the web page and code behind have somehow become lost.  The code behind pages can’t see their controls, which are placed in some mystery file thanks to the new Partial Class feature.

I tried ranaming the files, I tried cutting and then replacing the HTML that defines the controls, the only thing I found that works is to totally delete the file and recreate it exactly the same way.  What a pain.

The “What a pain” concept is the only constant in this process.

I am still not sure what is going on with project references. 

As far as I see, if there is a DLL in the bin directory, you don’t need a reference to that DLL.  But as soon as you clean out your bin, your project will break with error messages that are nothing like “Are you missing a reference?” because, of course you are, VS.Net just removes them all the time w/o asking you! 

There must be some new paradigm with how you are supposed to work with references, projects, and solutions, because trying to do it like you would in vs.net 2003 is causing a ton of problems.

Update: Almost as if VS was getting back at me, as soon as I posted this it took a crap and crashed.  🙂

From Asp.net 1.1 to 2.0: Parser Error – Could not load type 'xyz.Global'

As we were upgrading to run ASP.Net 2.0, we ran into this wonderful problem.  Your code seems to run fine until you push it to a server and you get this error:

 Parser Error

Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: Could not load type ‘xyz.Global’.
Source Error:

Line 1:  <%@ Application Inherits="xyz.Global" Language="VB" %>

Source File: /global.asax    Line: 1


Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42

 

There are 2 things I had to remember to do to get this to work.

1) We use a domain user for the aps.net worker process, so our site can access UNC files across the network.  In order for this user to have the ability to compile the app, you have to give them full rights to: c:WINDOWSMicrosoft.NETFrameworkv1.1.4322 and if you start running your apps in .net 2.0, you need to do the same for c:WINDOWSMicrosoft.NETFrameworkv2.0.50727.

2) If you have any web apps running in 1.1, there is a good chance that even thought you changed the iis setting to asp.net 2.X, you are still running against an app pool that is shared with another site using asp.net 1.1.  So, just create a new app pool, and have your newly 2.0 site setup to use that app pool.

Problem solved!

Running ASP.NET Development Server Without Virtual Path, From Root

I had been looking for a way to get the asp.net development server to run w/o a virtual directory settings.  It’s really very stupid and short sighted to not enable this.  Just about every project I have ever worked on has some paths coded into the HTML that are based on / being the root, not the application root.  Stupid.

 

Anyway, ScottGu posted these steps on his site, and it works.  Not bad!

Step 1: Select the “Tools->External Tools” menu option in VS or Visual Web Developer.  This will allow you to configure and add new menu items to your Tools menu.

 

Step 2: Click the “Add” button to add a new external tool menu item.  Name it “WebServer on Port 8080” (or anything else you want).

 

Step 3: For the “Command” textbox setting enter this value: C:WINDOWSMicrosoft.NETFrameworkv2.0.50727WebDev.WebServer.EXE (note: this points to the web-server that VS usually automatically runs).

 

Step 4: For the “Arguments” textbox setting enter this value: /port:8080 /path:$(ProjectDir)

 

Step 5: Select the “Use Output Window” checkbox (this will prevent the command-shell window from popping up.

 

 Once you hit apply and ok you will now have a new menu item in your “Tools” menu called “WebServer on Port 8080”.  You can now select any web project in your solution and then choose this menu option to launch a web-server that has a root site on port 8080 (or whatever other port you want) for the project.

 

You can then connect to this site in a browser by simply saying http://localhost:8080/.  All root based references will work fine.

 

Step 6: The last step is to configure your web project to automatically reference this web-server when you run or debug a site instead of launching the built-in web-server itself.  To-do this, select your web-project in the solution explorer, right click and select “property pages”.  Select the “start options” setting on the left, and under server change the radio button value from the default (which is use built-in webserver) to instead be “Use custom server”.  Then set the Base URL value to be: http://localhost:8080/