简体   繁体   中英

IHttpHandler is not getting called on MVC controller action method

I have defined a custom HTTP handler, to update the request header. But when I am calling http://localhost:52705/Home/Index . my custom HTTP handler is not getting called for that controller -> action request. I implemented as follow

public class TestHandler :  IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        return;
    }

    public bool IsReusable { get; private set; }
}

also, find the web.cofing entry for HTTPHandler

<system.webServer>
    <handlers>
      <add name="TestHandler" type=" mvc_app.Handler.TestHandler" path="*" verb="*"/>      
    </handlers>
    <modules>
      <remove name="FormsAuthenticationModule" />
    </modules>
  </system.webServer>

After spending one day if found one trick to achieve the same functionality as I want to implement using IHttpModule

Add custom HTTP module

public class TestModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.BeginRequest += OnBeginRequest;
    }

    static void OnBeginRequest(object sender, EventArgs a)
    {
        Debug.WriteLine("OnBeginRequest Called MVC");        
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }
}

Update web.config to register custom HTTP module

<system.webServer>
    <modules>
      <add name="TestModule" type="mvc_app.Handler.TestModule"/>          
    </modules>
 </system.webServer>

for the above solution still, I am trying to figure it out why my OnBeginRequest() method is getting called twice

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM