ASP.NET MVC 3 HttpPost动作方法未找到 [英] ASP.NET MVC 3 HttpPost action method not found

查看:258
本文介绍了ASP.NET MVC 3 HttpPost动作方法未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的表格,职位,一个 HttpPost 操作方法,该方法返回其对应的视图。我的问题是,我得到一个 404未找​​到错误。奇怪的是,如果我改变了形式方法和行动上法属性的 GET ,那么它的工作原理,并显示TestMethod的视图。

我似乎失去了一些东西,使用 POST ,但我在其他控制器岗位正常工作(例如帐户登录和注册)。注意使用AllowAnonymous 属性自定义属性,以便能够指定允许匿名访问的控制器或动作,而不是指定(通过授权 attr)使用需要授权的控制器或动作。我想没有什么是不可能的,但我不认为这有什么与我的问题。对什么是错的任何想法?

FORM:

  @using(Html.BeginForm(TestMethod的,测试,FormMethod.Post,新{@id =TESTFORM})){
    <&字段集GT;
        <传奇>测试表及LT; /传说>
        <输入类型=提交值=提交/>
    < /字段集>
}

控制器动作:

  [使用AllowAnonymous]
[HttpPost]
公众的ActionResult TestMethod的(){
    返回查看();
}

查看:

 < H2> TestMethod的< / H>
< P> HttpPost方法是成功的< / P>

REGISTER ROUTES方法从Global.asax.cs中:

 公共静态无效的RegisterRoutes(RouteCollection路线){
        routes.IgnoreRoute(的favicon.ico);
        routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);        // 关于
        routes.MapRoute(
            关于,//路线名称
            左右,//带参数的URL
            新{控制器=家,行动=关于} //参数默认
        );        // 常问问题
        routes.MapRoute(
            FAQ,//路线名称
            常见问题解答,//带参数的URL
            新{控制器=家,行动=FAQ} //参数默认
        );        //词汇表
        routes.MapRoute(
            词汇表,//路线名称
            词汇表,//带参数的URL
            新{控制器=家,行动=词汇表} //参数默认
        );        //注册
        routes.MapRoute(
            注册,//路线名称
            登记,//带参数的URL
            新{控制器=账户,行动=注册} //参数默认
        );        // 登录
        routes.MapRoute(
            登录,//路线名称
            登录/(编号),// URL带参数
            新{控制器=账户,行动=登录,ID = UrlParameter.Optional} //参数默认
        );
        routes.MapRoute(
            登录,//路线名称
            登录/(编号),// URL带参数
            新{控制器=账户,行动=登录,ID = UrlParameter.Optional} //参数默认
        );        //默认
        routes.MapRoute(
            默认,//路线名称
            {控制器} / {行动} / {ID},// URL带参数
            新{控制器=家,行动=索引,ID = UrlParameter.Optional} //参数默认
        );}

AUTHORIZE ATTRIBUTE code:

  // AllowAnonymousAttribute类
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method,的AllowMultiple = FALSE,继承= TRUE)]
公共密封类AllowAnonymousAttribute:属性{}// GlobalAuthorize类
公共密封类GlobalAuthorize:AuthorizeAttribute {
    公共覆盖无效OnAuthorization(AuthorizationContext filterContext){
        布尔skipAuthorization =
            filterContext.ActionDescriptor.IsDefined(typeof运算(AllowAnonymousAttribute),真)||
            filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof运算(AllowAnonymousAttribute),TRUE);        如果base.OnAuthorization(filterContext)(skipAuthorization!);
    }
}// RedirectAuthorizeAttribute类
公共类RedirectAuthorizeAttribute:AuthorizeAttribute {
    公共字符串的redirectUrl {搞定;组; }    保护覆盖无效HandleUnauthorizedRequest(AuthorizationContext filterContext){
        filterContext.Result =新RedirectResult(的redirectUrl);
    }
}

全局过滤器:

 公共静态无效RegisterGlobalFilters(GlobalFilterCollection过滤器){
    filters.Add(新RequireHttpsAttribute());
    filters.Add(新GlobalAuthorize());
    filters.Add(新HandleErrorAttribute());
}

ROUTE重写规则:

 <&改写GT;
  <规则与GT;
    <! - 块到一个网站,没有主机头组发出的所有请求。 - >
    <规则名称=失败错误的请求stopProcessing =真>
      <*匹配URL = />
      <条件>
        <添加输入={HTTP_HOST}模式=localhost的否定=TRUE/>
      &所述; /条件>
      <作用TYPE =AbortRequest/>
    < /规则>
    <! - 卸下所有传入请求斜线。 - >
    <规则名称=删除斜杠stopProcessing =false的>
      <匹配URL =/ $(*)/>
      <条件>
        <添加输入={} REQUEST_FILENAME使用MatchType =ISFILE否定=TRUE/>
        <添加输入={} REQUEST_FILENAME使用MatchType =IsDirectory否定=TRUE/>
      &所述; /条件>
      <作用TYPE =重定向redirectType =永久URL ={R:1}/>
    < /规则>
    <! - 转换为全部小写的所有请求。 - >
    <规则名称=转换成小写stopProcessing =false的>
      <*。[A-Z] *匹配URL = IGNORECASE =FALSE/>
      <作用TYPE =重定向URL ={ToLower将:{R:0}}redirectType =永久/>
    < /规则>
    <! - 任何与(HTTP_HOST与结肠癌)的URL(HTTPS == OFF)和 - >用于开发测试。 - >
    <规则名称=发展重定向到HTTPS启用=真stopProcessing =真>
      <*匹配URL = IGNORECASE =真/>
      <条件logicalGrouping =MatchAlltrackAllCaptures =真>
        <添加输入={} HTTPS模式=^ $ OFF/>
        <添加输入={HTTP_HOST}模式=?(?[^ /:] *):[^ /] * />
      &所述; /条件>
      <作用TYPE =重定向URL =htt​​ps://开头{C:1}:44300 {URL}/>
    < /规则>
    <! - 重定向任何HTTP请求到HTTPS。 - >
    <规则名称=重定向到HTTPSsto​​pProcessing =真>
      <匹配URL =(。*)/>
      <条件>
        <添加输入={} HTTPS模式=^ $ OFFIGNORECASE =真/>
      &所述; /条件>
      <作用TYPE =重定向URL =htt​​ps://开头{HTTP_HOST} / {R:1}redirectType =永久/>
    < /规则>
  < /规则>
< /重写>


解决方案

我想我终于找到了罪魁祸首。首先,我要承认,现在是什么问题是知道,这个问题的标题是不是很准确。这个问题主要涉及在Web.config中的规则重写。直到听取别人的意见对这个问题,我已经完全忘记了有关规则的重写,这就是为什么我没有查出来再开始。

总之,问题是重写URL以全部小写的规则。我知道我的帐户注册和登录表单都工作正常,所以我检查出来,发现自己的 Html.BeginForm 声明是无参数,其中,显然会导致一个小写的URL为产生的。我想为我的测试方法参数的POST请求,和它的工作。然后,我尝试使用在 Html.BeginForm 语句行动和控制器参数,但是这一次我进入他们为小写字符串: Html.BeginForm( TestMethod的,测试...)。果然,它也工作得很好,而页面的源代码显示的形式行动小写。

要解决我的问题,我不得不设置一个条件不匹配 POST 请求:&LT;添加输入={REQUEST_METHOD}使用MatchType =模式模式=POST否定=TRUE/&GT; 。请注意,这个问题是没有明确的规则,小写字母,而是在 POST 请求被重定向。我发现,讨论<$的问题的一种博客 C $ C> POST 重定向被转换为 GET 和造成的错误,这是exaclty我正经历。这是一对夫妇岁,但显然它仍然是相关信息。

在任何情况下,我现在备份和运行。感谢所有谁在他们的两分钱扔去。

P.S。当我关闭浏览器选项卡和结束我的搜索,我想我会链接到本SO <一个href=\"http://stackoverflow.com/questions/5994261/in-the-iis7-url-rewrite-module-can-i-specify-in-a-redirect-rule-to-not-apply-to\">question,因为它肯定是与我的问题。

I have a simple form that posts to an HttpPost action method, which returns its corresponding view. My issue is that I'm getting a 404 Not Found error. Oddly enough, if I change the form method and the attribute on the action method to a GET, then it works and the TestMethod view is displayed.

It seems I'm missing something for using POST, but my posts in other controllers work fine (e.g. account login and registration). Note the AllowAnonymous attribute is a custom attribute to be able to specify the controllers or actions that allow anonymous access, as opposed to specifying (via the Authorize attr) the controllers or actions that require authorization. I guess nothing is impossible, but I don't think that has anything to do with my issue. Any thoughts on what is wrong?

FORM:

@using (Html.BeginForm("TestMethod", "Test", FormMethod.Post, new { @id = "testForm" })) {
    <fieldset>
        <legend>Test Form</legend>
        <input type="submit" value="Submit" />
    </fieldset>
}

CONTROLLER ACTION:

[AllowAnonymous]
[HttpPost]
public ActionResult TestMethod() {
    return View();
}

VIEW:

<h2>TestMethod</h2>
<p>HttpPost method was successful.</p>

REGISTER ROUTES METHOD FROM Global.asax.cs:

public static void RegisterRoutes(RouteCollection routes) {
        routes.IgnoreRoute("favicon.ico");
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        // About
        routes.MapRoute(
            "About", // Route name
            "about", // URL with parameters
            new { controller = "Home", action = "About" } // Parameter defaults
        );

        // Faq
        routes.MapRoute(
            "Faq", // Route name
            "faq", // URL with parameters
            new { controller = "Home", action = "Faq" } // Parameter defaults
        );

        // Glossary
        routes.MapRoute(
            "Glossary", // Route name
            "glossary", // URL with parameters
            new { controller = "Home", action = "Glossary" } // Parameter defaults
        );

        // Register
        routes.MapRoute(
            "Register", // Route name
            "register", // URL with parameters
            new { controller = "Account", action = "Register" } // Parameter defaults
        );

        // LogIn
        routes.MapRoute(
            "LogIn", // Route name
            "login/{id}", // URL with parameters
            new { controller = "Account", action = "LogOn", id = UrlParameter.Optional } // Parameter defaults
        );
        routes.MapRoute(
            "LogOn", // Route name
            "logon/{id}", // URL with parameters
            new { controller = "Account", action = "LogOn", id = UrlParameter.Optional } // Parameter defaults
        );

        // Default
        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

}

AUTHORIZE ATTRIBUTE CODE:

// AllowAnonymousAttribute class
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class AllowAnonymousAttribute : Attribute { }

// GlobalAuthorize class
public sealed class GlobalAuthorize : AuthorizeAttribute {
    public override void OnAuthorization(AuthorizationContext filterContext) {
        bool skipAuthorization = 
            filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) || 
            filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true);

        if (!skipAuthorization) base.OnAuthorization(filterContext);
    }
}

// RedirectAuthorizeAttribute class
public class RedirectAuthorizeAttribute : AuthorizeAttribute {
    public string RedirectUrl { get; set; }

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) {
        filterContext.Result = new RedirectResult(RedirectUrl);
    }
}

GLOBAL FILTERS:

public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
    filters.Add(new RequireHttpsAttribute());
    filters.Add(new GlobalAuthorize());
    filters.Add(new HandleErrorAttribute());
}

ROUTE REWRITING RULES:

<rewrite>
  <rules>
    <!-- Block all requests made to a website that do not have the host header set. -->
    <rule name="Fail bad requests" stopProcessing="true">
      <match url=".*" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="localhost" negate="true" />
      </conditions>
      <action type="AbortRequest" />
    </rule>
    <!-- Remove trailing slash from all incoming requests. -->
    <rule name="Remove trailing slash" stopProcessing="false">
      <match url="(.*)/$" />
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
      </conditions>
      <action type="Redirect" redirectType="Permanent" url="{R:1}" />
    </rule>
    <!-- Convert all requests to all lowercase. -->
    <rule name="Convert to lowercase" stopProcessing="false">
      <match url=".*[A-Z].*" ignoreCase="false" />
      <action type="Redirect" url="{ToLower:{R:0}}" redirectType="Permanent" />
    </rule>
    <!-- Any URL with (HTTPS == OFF) and (HTTP_HOST with colon) -> use for development testing. -->
    <rule name="Development redirect to HTTPS" enabled="true" stopProcessing="true">
      <match url=".*" ignoreCase="true" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
        <add input="{HTTPS}" pattern="^OFF$" />
        <add input="{HTTP_HOST}" pattern="([^/:]*?):[^/]*?" />
      </conditions>
      <action type="Redirect" url="https://{C:1}:44300{URL}" />
    </rule>
    <!-- Redirect any HTTP request to HTTPS. -->
    <rule name="Redirect to HTTPS" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="^OFF$" ignoreCase="true" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

解决方案

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.

这篇关于ASP.NET MVC 3 HttpPost动作方法未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆