0

What is the best way to override all GET requests in .NET MVC and pipe them to a single controller's action?

I want only POST requests to go through the standard pipeline, e.g.

GET /Eating/Apples -> /GlobalProcessor/Index
POST /Eating/Apples -> /Eating/Apples

If .NET filters are your answer, then how would I accomplish that without using RedirectToAction(), as I need to maintain the URL structure. Meaning,

GET /Eating/Apples

Would be processed by /GlobalProcessor/Index but appear to the client as /Eating/Apples

And if you are wondering why -- it is for a dynamic AJAX processing backend that I'm implementing.

Allison A
  • 5,575
  • 6
  • 28
  • 32

2 Answers2

3

You could create a route that matches everything and then have an IRouteConstraint that matches when the request method is GET:

routes.MapRoute("Get", 
"{*path}", 
new {controller = "GlobalProcessor", action = "Index" }, 
new {isGet = new IsGetRequestConstraint()} );

With IsGetRequestConstraint being:

public class IsGetRequestConstraint: IRouteConstraint 
{ 
  public bool Match ( HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection ) 
  { 
    return httpContext.Request.Method == "GET"; 
  } 
}
Nathan Anderson
  • 6,768
  • 26
  • 29
0

You could try adding something like this in your routes config

routes.MapRoute(
    "GlobalProcessorThingy",
    "{*url}",
    new { controller = "GlobalProcessor", action = "Index" }
);

I tailored an answer from this SO question

Community
  • 1
  • 1
BlakeH
  • 3,354
  • 2
  • 21
  • 31