views:

81

answers:

1

Hi all,

So, I've run into a weird issue with a route in my application. For some reason it just won't match. It looks like {controller}/Comments/Put and it has corresponding constraints to make sure it gets into the right controller, in this case "Misc". When I post to the route it just doesn't match...

I plugged in the RouteDebugger library and it says "No Match!", BUT, when I look at the table of routes, the one that's crapping out has a green "True" under the "Match" column, so I'm at a slight loss. How can it match and not match at the same time?

I've gone as far as commenting out ALL other routes for the application and I still get the same result.

The one time it did work is when I changed the route to something stupidly long, but then I got another error that says I can't Post to the action, which was because it was decorated with [HttpPut], BUT, I had the HttpMethodOverride as part of the post... Anyway, that's another stupid bug that shouldn't even be happening, but then again, that can be said for all bugs.

Idk if this makes any difference, it shouldn't..., but the action that needs to be called is a RedirectToRouteResult action.

EDIT: I've also tried putting the route on the very top of the list so it get's called first, still no change...

Anyway, if someone can point me to why the route is failing, I would really appreciate it!

Thanks in advance!

EDIT 2: To clarify a little bit more, the application is essentially for posting short messages and then commenting on those messages.

Posting a message works fine through an Ajax controller with a route of Ajax/Post/Put which matches {controller}/Post/Put. As you may notice the routes are very similar, but they are not conflicting with one another. Each route has a controller constraint in the form of controller = new ControllerConstraint("Ajax") or controller = new ControllerConstraint("Misc").

EDIT 3: To clarify a little bit more again, here's the controller/action that should be called and the route in Global.asax:

MiscController : BaseController {
    [HttpPut, ValidateAntiForgeryToken]
    public RedirectToRouteResult PutComment(
        [Bind(Prefix = "Comment", Include = "Text")] Comment Comment) {
        // Do stuff...
    }
}

Routes.MapRoute("Misc (Put Comment)", "{controller}/Comments/Put", new {
    action = "PutComment"
}, new {
    controller = new ControllerConstraint("Misc")
});

EDIT 4: Hard-writing the route as follows has no effect:

Routes.MapRoute("Misc (Put Comment)", "Misc/Comments/Put", new {
    controller = "Misc",
    action = "PutComment"
});

EDIT 5: I've gone through and simplified all of the routes, no more params in the the url and no more constraints. So, the routes look like:

Routes.MapRoute("Misc (Comments)", "Misc/Comments", new {
    controller = "Misc",
    action = "Comments"
});

So, now that the route is completely hard-coded there should be no discrepancies about no matching routes. Plain and simple... or is it...

After trying that, it sort of worked, I was now getting yelled at that POST is not allowed, which was true because the action was decorated with PUT, BUT, the Http override was being sent across as it should have, but the framework completely ignored it(?).

So, moving on, I decided to retry the exact same post attempt, but this time enabling the route debugger gave me the following: The route was still not matching, YET in the routes table, it's listed in green as matching. CONTRADICTIONS up the a**!!!

So, swallowing yet another BS error, I decided to disable every other route as well as every other method from all other controllers.

Guess what, I STILL GOT THE SAME RESULTS! It's as if the framework goes completely SUPER MEGA HERP-A-DE-DERP on me...

I'm highly frustrated. I've written apps with far more complexity than this simple post/comment app and I've never ran into so much difficulties...

I just don't know anymore...

::goes to a corner, curls up and starts crying::

A: 

Ok, so I figured it out, I think, but either way it works now.

My first mistake was in that I was suppressing an error coming from the database. The action had been called, but was crashing and suppressing the database error so from my point of view I was assuming that the action wasn't being called because I never saw the data change.

My second mistake that I wasn't redirecting properly, or specifically I wasn't specifying the controller for the redirect. In turn the redirect was trying to redirect to an action that didn't exist within the same controller.

I did however, and this is where it gets a tad contradictory, get an HttpVerb exception a couple of times. I can only assume that I was calling the original action and then redirecting to an existing action, but the redirect was retaining the HttpVerb thus causing the exception on the second action. This however still seems like a BS explanation which is probably wrong, but it's the best way I can make sense of it.

Lastly, I noticed that if a route is configured for controller A, but the action is actually in controller B, you will get an exception that makes it look like it was processing an "action" but failed. In other words, it will mislead you to think that you are hitting an action when you really aren't.

Anyway, hope this helps someone in the future.

Alex