ASP.NET MVC 3 HttpPost action method not found

泪湿孤枕 提交于 2019-12-05 16:08:06
neizan

I think I finally found the culprit. First of all, I'll concede that, knowing now what the issue was, the title of this question isn't very accurate. The problem mostly dealt with a rule rewrite in Web.config. Until responding to others' comments for this question, I had completely forgotten about the rule rewrites, which is why I hadn't checked them out further to begin with.

Anyway, the issue was a rule that rewrote URLs to all lowercase. I knew my account registration and login forms were working fine, so I checked them out and noticed that their Html.BeginForm statements were parameterless, which, evidently results in a lowercase url being generated. I tried parameterless POST request for my test method, and it worked. Then, I tried using parameters for action and controller in the Html.BeginForm statement, but this time I entered them as lowercase strings: Html.BeginForm("testmethod", "test"...). Sure enough, it, too, worked just fine, and the page source showed the form action as lowercase.

To fix my problem, I just had to set a condition to not match POST requests: <add input="{REQUEST_METHOD}" matchType="Pattern" pattern="POST" negate="true" />. Note that the issue was not the lowercase rule specifically, but rather that the POST request was being redirected. I found one blog that discusses the issue of POST redirects being converted to GETs and resulting in errors, which is exaclty what I was experiencing. It's a couple years old, but apparently it's still pertinent info.

At any rate, I'm now back up and running. Thanks to all who threw in their two cents.

P.S. As I'm closing browser tabs and concluding my search, I figured I'd link to this SO question, as it is definitely related to my issue.

I tested your code just now. I initially got redirected for not using HTTPS so I disabled this attribute, but after that your code worked.

Here's my logical deducting...

  • If HTTPS was the problem, you wouldn't be getting a 404.
  • If you weren't logged in, you would be redirected to the login page.

The only thing I can think of is that your controller is either not named "TestController" or that your controller is in an area and you forget to supply this area with the BeginForm. Is it one of these by any chance?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!