Jack @ ASP.NET

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Entries for the ‘asp.net’ Category

asp.net error: Operation is not valid due to the current state of the object

ASP.NET requests that have lots of form keys, files, or JSON payload receive an error response from the server. The Application log on the server has a Warning entry with a Source that is a specific version of ASP.NET, and an Event ID of 1309. The event log contains one of the following messages:

Message 1:

Application information:
    Application domain: /LM/W3SVC/1/ROOT/<App Domain>
    Trust level: Medium
    Application Virtual Path: <VDIR Path>
    Application Path: <App Path>
    Machine name: <Machine Name>
Process information:
    Process ID: 0001
    Process name: w3wp.exe
    Account name: IIS APPPOOL\DefaultAppPool
Exception information:
    Exception type: HttpException
    Exception message: The URL-encoded form data is not valid.
   at System.Web.HttpRequest.FillInFormCollection()
   at System.Web.HttpRequest.get_Form()
   at System.Web.HttpRequest.get_HasForm()
   at System.Web.UI.Page.GetCollectionBasedOnMethod(Boolean dontReturnNull)
   at System.Web.UI.Page.DeterminePostBackMode()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


Message 2:

Application information:
    Application domain: /LM/W3SVC/1/ROOT/<App Domain>
    Trust level: Medium
    Application Virtual Path: <VDIR Path>
    Application Path: <App Path>
    Machine name: <Machine Name>
Process information:
    Process ID: 0001
    Process name: w3wp.exe
    Account name: IIS APPPOOL\DefaultAppPool
Exception information:
   Exception type: InvalidOperationException
    Exception message: Operation is not valid due to the current state of the object.
   at System.Web.HttpRequest.FillInFilesCollection()
   at System.Web.HttpRequest.get_Files()
   at FileUpload.Page_Load(Object sender, EventArgs e)
   at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
   at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
   at System.Web.UI.Control.OnLoad(EventArgs e)
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint

 

to resolve this problem, you should change your web.config

<configuration>
  <appSettings>
    <add key="aspnet:MaxHttpCollectionKeys" value="5000" />
  </appSettings>
</configuration>

Setting an aspnet.config File per Application Pool

The aspnet.config file is a little known config file which is supported by ASP.NET 2.0 and greater.  Generally it lives in the root of the framework folder, for example:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet.config
C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet.config
C:\Windows\Microsoft.NET\Framework64\v2.0.50727\aspnet.config
C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet.config

This config file is further leveraged in ASP.NET 4.0 for concurrency and threading.  For example, you can set maxConcurrentRequestsPerCPU, maxConcurrentThreadsPerCPU and requestQueueLimit, in addition to the previous runtime settings.

In Windows Server 2008 R2 (IIS 7.5) support was added to allow different settings per application pool.  Where previously the settings had to be applied to the whole framework version, now they can be specific to each app pool.  It does this by allowing you to create a custom aspnet.config file per app pool.  You can save them wherever you want on disk and IIS will pick them up when the app pool starts.

Note that the framework aspnet.config file is still used so only differences per app pool need to be set here.

Here’s an example of what the file can contain:

<?xml version="1.0" encoding="UTF-8" ?>

<configuration>

    <runtime>

        <legacyUnhandledExceptionPolicy enabled="false" />

        <legacyImpersonationPolicy enabled="true"/>

        <alwaysFlowImpersonationPolicy enabled="false"/>

        <SymbolReadingPolicy enabled="1" />

        <shadowCopyVerifyByTimestamp enabled="true"/>

    </runtime>

    <startup useLegacyV2RuntimeActivationPolicy="true" />

  <system.web>

    <applicationPool

        maxConcurrentRequestsPerCPU="5000"

        maxConcurrentThreadsPerCPU="0"

        requestQueueLimit="5000" />

  </system.web>

</configuration>

Manage a checkbox via jQuery

the checkBox in html should be

<input type="checkbox" class="myCheckBox" />

Bind a click event to a checkbox

$(".myCheckBox").click(myCheckboxEvent);

The click event like this

function myCheckboxEvent() {
    alert($(this).is(':checked')); // alert the checked value
}

You can see, it is quite easy to ‘get’ the checkbox sender via jQuery -‘this’

Add NHibernate Configuration in web.config, and reuse an existing connectionstring

When we use nHibernate in asp.net, most time we use a nhibernate.cfg.xml to define the database connection. And for some scenarioes, we may also have a database connection defined in web.config. So, there might be 2 definitions for the same database. Here is the way to combine the 2 definitions and in web.config.

in web.config

 1:  <configuration>
 2:    <configSections>
 3:      <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
 4:    </configSections>
 5:    <appSettings>
 6:    </appSettings>
 7:    <connectionStrings>
 8:      <add name="MyDefaultDatabase" connectionString="Data Source=......" />
 9:    </connectionStrings>
10:    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
11:      <session-factory>
12:        <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
13:        <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
14:        <property name="connection.connection_string_name">MyDefaultDatabase</property>
15:        <property name="command_timeout">300</property>
16:        <property name="show_sql">false</property>
17:      </session-factory>
18:    </hibernate-configuration>
19:  ……………

 

and pay attention,

  1. The secion name must be ‘hibernate-configuration’ , or it will not work. Very tricky.
  2. The type must be ‘NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" , also very tricky.
  3. Use connection.connection_string_name if you want to reuse an existing database connection.
  4. in your code, use
  5. new NHibernate.Cfg.Configuration().Configure();
    instead of given a nhibernate.cfg.xml file parameter.

 

Now, you only have 1 database connections in your web.config~

HTML vs XHTML

HTML syntax is the format suggested for most authors. It is compatible with most legacy Web browsers. If a document is transmitted with an HTML MIME type, such as text/html, then it will be processed as an HTML document by Web browsers. This specification defines version 5 of the HTML syntax, known as “HTML5″.

XHTML syntax is an application of XML. When a document is transmitted with an XML MIME type, such as application/xhtml+xml, then it is treated as an XML document by Web browsers, to be parsed by an XML processor. Authors are reminded that the processing for XML and HTML differs; in particular, even minor syntax errors will prevent a document labeled as XML from being rendered fully, whereas they would be ignored in the HTML syntax. This specification defines version 5 of the XHTML syntax, known as “XHTML5″.

The DOM, the HTML syntax, and XML cannot all represent the same content. For example, namespaces cannot be represented using the HTML syntax, but they are supported in the DOM and in XML. Similarly, documents that use the noscript feature can be represented using the HTML syntax, but cannot be represented with the DOM or in XML. Comments that contain the string “–>” can only be represented in the DOM, not in the HTML and XML syntaxes.

The term MIME type is used to refer to what is sometimes called an Internet media type in protocol literature. The term media type in this specification is used to refer to the type of media intended for presentation, as used by the CSS specifications.

jQuery script to scroll to the top of window

$('html, body').animate({ scrollTop: 0 }, 'slow');