Firoso
Firoso

Reputation: 6685

using ASP.NET MVC, is there a way to capture all POST requests regardless of URL to a host?

I'm having trouble debugging an MVC app that I'm expecting a relying part to POST to.

Is there an easy way to configure an MVC 4 application to intercept all POST requests so I can see what URL it is trying to post to and what the response is?

Upvotes: 4

Views: 683

Answers (1)

GalacticCowboy
GalacticCowboy

Reputation: 11769

Normally, a global filter would be what you want. However, since that would not trigger until after the route was determined, it might not help you in this situation.

One thing you could try is to add an Application_BeginRequest method in global.asax. It doesn't even have to do anything. Just set a breakpoint in it, and inspect Request.AppRelativeCurrentExecutionFilePath (or the other members of Request) to see what the inbound path is.

    protected void Application_BeginRequest()
    {
        if (Request.HttpMethod.Equals("post", StringComparison.InvariantCultureIgnoreCase))
        { // Breakpoint here

        }
    }

A "global filter" is the preferred method for injecting behaviors, etc. into the pipeline. MVC already implements some of these, and you can also extend or implement your own.

One concrete example that I implemented last year: client site has feature to expire password. Great; works fine, when the user changes it immediately when told to do so. But they figured out they could just navigate to another portion of the site, and it didn't prompt again until next time they logged in. So we added a global filter that checked whether the password was expired (and whether we'd already checked in this session) and, if so, redirected to the "change password" screen. After changing it, they could return to wherever they were before.

Here's the drawback to this, though, for figuring out a routing or binding issue: the routing engine has already evaluated the request before your global filter can get it. If it fails to identify a target action, your filter won't even be hit. So in this case, a lower-level feature like Application_BeginRequest is your only realistic option.

Upvotes: 5

Related Questions