-->

Using a class defined in App_Code in web.config

2020-08-01 09:39发布

问题:

I have a class named CustomWebAuditEvent defined in App_Code/CustomWebAuditEvent.vb and I'm trying to use it in a eventMappings of my Web.Config

<healthMonitoring enabled="true">
  <eventMappings>
    <clear />
    <add name="HandeledException"
         type="CustomWebAuditEvent"
     />
  </eventMappings>
....

I got the error message:

Could not load type 'CustomWebAuditEvent'

So I tried

  • type="CustomWebAuditEvent, App_Code"
  • type="CustomWebAuditEvent, App_Code.CustomWebAuditEvent"
  • type="CustomWebAuditEvent, __Code"
  • type="CustomWebAuditEvent, __Code.CustomWebAuditEvent"

and all I got is this error message :

Could not load file or assembly 'App_Code' or one of its dependencies. The system cannot find the file specified.

How could I make this work?

回答1:

Try moving CustomWebAuditEvent out of App_code and just placing the class in the same directory as web.config. Then in web.config, reference it as ClassName.CustomWebAuditEvent

I have seen this happen before, although I don't know what causes App_Code to not be recognized.



回答2:

I moved my CustomWebAuditEvent class into a separated class library and put the compiled assembly in my bin folder.

Note: I found this other question that describe the exact same problem.