C# 在 web.config 中注册 httpModules

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1446097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:36:17  来源:igfitidea点击:

registering httpModules in web.config

c#asp.net

提问by Praesagus

I am trying to register a custom HttpHandler in the web.config file. MSDN'sexample shows an entry that is commented out...um which doesn't work so well. When I uncomment I receive a Could not load file or assembly 'HttpModule.cs' or one of its dependencies. The system cannot find the file specified.error. The file name is HttpModule.cs and the class name is MyHttpModule. There is no explicit namespace yet.

我正在尝试在 web.config 文件中注册自定义 HttpHandler。MSDN 的示例显示了一个被注释掉的条目……嗯,效果不佳。当我取消注释时,我收到一个Could not load file or assembly 'HttpModule.cs' or one of its dependencies. The system cannot find the file specified.错误。文件名为 HttpModule.cs,类名为 MyHttpModule。目前还没有明确的命名空间。

<httpModules>
     <add name="MyHttpModule" type="MyHttpModule, HttpModule" />
<httpModules>

I am sure I am overlooking the obvious. Do I need to specify the file path somewhere? Thanks in advance.

我确定我忽略了显而易见的事情。我需要在某处指定文件路径吗?提前致谢。

采纳答案by Praesagus

I am still learning much of the terminology and needed it spelled out for me. In case any of you need the same...

我仍在学习很多术语,需要为我详细说明。如果你们中的任何人需要相同的...

As an example if:

例如,如果:

  • The base class name and project name is TestSite
  • The namespace my class is in is BusinessLogic
  • The class name is PageAuthorization
  • 基类名称和项目名称是 TestSite
  • 我的班级所在的命名空间是 BusinessLogic
  • 班级名称是 PageAuthorization

in the Web.config file...

在 Web.config 文件中...

<configuration> 
 <system.web> 
  <httpModules> 
   <add type= "BusinessLogic.PageAuthorization, TestSite" name="PageAuthorization" /> 
  </httpModules> 
 </system.web> 
</configuration>

In my case I had to mark my class with IHttpModule. The definition of the class would look like:

就我而言,我必须用IHttpModule. 类的定义如下所示:

public class PageAuthorization : IHttpModule

回答by Mitchel Sellers

The typevalue is in the format of {Class}, {assembly}.

type值的格式为{Class}, {assembly}.

So in your case, it should be MyHttpModule, MyDllName

所以在你的情况下,应该是 MyHttpModule, MyDllName

Where MyDllNameis the name of the compiled DLL.

MyDllName编译后的 DLL 的名称在哪里。

回答by kachingy123

To add to this, it is important to be sure that the .dll you are trying to use is in the project bin folder. I had the problem of making my module in a separate project, but forgetting to push over the .dll file.

为此,确保您尝试使用的 .dll 位于项目 bin 文件夹中非常重要。我在单独的项目中制作我的模块时遇到了问题,但忘记推送 .dll 文件。

回答by Sudhanshu Mishra

Note for people landing here looking for configuration for IIS 7 and above running in Integrated Mode:

登陆此处寻找以集成模式运行的 IIS 7 及更高版本的配置的注意事项:

<configuration> 
 <system.webServer> 
  <modules> 
   <add type= "BusinessLogic.PageAuthorization, TestSite" name="PageAuthorization" /> 
  </modules> 
 </system.webServer> 
</configuration>

回答by Rishi

If you are loading Custom module from .Net 4.0 GAC and IIS 6/7 classic mode you have to use below code

如果您从 .Net 4.0 GAC 和 IIS 6/7 经典模式加载自定义模块,则必须使用以下代码

 <system.web>
  <httpModules>
  <add name="ClientHttpModule" type="ClientServHttpModule.ClientHttpModule,ClientServHttpModule,Version=1.0.0.0, Culture=neutral, PublicKeyToken=3af8d8e2f9e8b6c3" />
      </httpModules>
 </system.web>

ClientServHttpModule is namespace of your custom module. Public key token you can get it from sn.exe app.

ClientServHttpModule 是自定义模块的命名空间。您可以从 sn.exe 应用程序获取公钥令牌。

if you are running in Integrated mode.use below code

如果您在集成模式下运行。请使用下面的代码

<system.webServer>
        <modules>
            <add name="ClientHttpModule" type="ClientServHttpModule.ClientHttpModule,ClientServHttpModule,Version=1.0.0.0, Culture=neutral, PublicKeyToken=3af8d8e2f9e8b6c3" />
        </modules>
    </system.webServer>