Asp .NET MVC中的HttpPatch请求

时间:2016-03-14 18:27:54

标签: asp.net asp.net-mvc httprequest http-patch

我试图在HttpPatch注释的控制器中使用方法。我的问题是,当我触发该方法时,我遇到404错误,找不到资源。当我按下" Patch"似乎我不满足方法需求并发送HttpPost请求。按钮而不是HttpPatch请求。如果有人知道如何使用HttpPatch注释触发我的Patch方法。这是我的控制器:

[HttpPatch]
public ActionResult Patch()
{

     return View();
}

以下是我的观点:

@model Practice.Models.PatchModel


<h2>Index</h2>
@using (Html.BeginForm("Patch", "Home"))
{
    <div>
        @Html.Label("Age")
        <div>
            @Html.TextBoxFor(model => model.age)
        </div>
    </div>
    <div>
        @Html.Label("ID")
        <div>
            @Html.TextBoxFor(model => model.id)
        </div>
    </div>
    <input type="submit" value="Patch" />
}

这是我的模特:

namespace Practice.Models
{
    public class PatchModel
    {
        public int age { get; set; }
        public int id { get; set; }
    }
}

1 个答案:

答案 0 :(得分:1)

以下是使用Ajax的问题​​的解决方案:

@model Practice.Models.Account.RegistrationViewModel    
<html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Login</title>
        <script src="~/scripts/jquery-2.2.1.min.js"></script>
        <script src="~/scripts/jquery.unobtrusive-ajax.min.js"></script>
    </head>
    <body>
        <div>
            @using (Ajax.BeginForm("ProcessLogin", "Account", new AjaxOptions() { HttpMethod = "PATCH" }))
            {
                @Html.LabelFor(x => x.Login)
                @Html.EditorFor(x => x.Login)
                <br />
                @Html.LabelFor(x => x.Password)
                @Html.PasswordFor(x => x.Password)
                <input type="submit" value="Login" />
            }
        </div>
    </body>
    </html>
控制器中的

和动作方法:

[HttpPatch]
public ActionResult ProcessLogin()
{
     //some code ...
     return View();
}