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

I'm having trouble debugging an MVC application, which I expect from the relying part to POST.

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

+4
source share
1 answer

Usually a global filter will be what you want. However, since this will not work until a route is determined, this may not help you in this situation.

One thing you can try is to add the Application_BeginRequest method to global.asax. He has nothing to do. Just set a breakpoint in it and check Request.AppRelativeCurrentExecutionFilePath (or other query members) to find out what the incoming path is.

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

A " global filter " is the preferred method for entering behavior, etc. into the conveyor. MVC already implements some of them, and you can also expand or implement your own.

One specific example that I implemented last year: on the client site, there is a function for password expiration. Large; works great when the user immediately changes it when they tell him that. But they found out that they can simply go to another part of the site, and he did not request again until the next time they entered the system. Thus, we added a global filter that checked whether the password had expired (and if we had already been verified in this session) and, if so, redirected to the "change password" screen. After his change, they could return to where they were before.

Here's the drawback of this, however, to clarify the problem of routing or binding: the routing mechanism has already evaluated the request before your global filter can receive it. If he cannot determine the target action, your filter will not even suffer. So in this case, a lower level function like Application_BeginRequest is your only realistic option.

+5
source

All Articles