I've created a new MVC web app in VS.NET 2010. I can access localhost/Home/Contact without issue. That's a method that comes built into the example app.
我在VS.NET 2010中创建了一个新的MVC Web应用程序。我可以毫无问题地访问localhost / Home / Contact。这是一个内置于示例应用程序中的方法。
I added another method:
我添加了另一种方法:
[HttpPost]
public ActionResult MyMethod(ClassA content)
{
return new HttpStatusCodeResult(204);
}
When I try to access this method using:
当我尝试使用以下方法访问此方法时:
localhost/Home/MyMethod
本地主机/主页/的MyMethod
I get a 404 error. I've tried directly in the browser (GET) and also through POSTing. Any idea what might be wrong?
我收到404错误。我已经尝试直接在浏览器(GET)和POSTing。知道什么可能是错的吗?
2 个解决方案
#1
1
The HttpPost
attribute indicates that the action can only be accessed through a POST
request, it protects you from other request types (GET, PUT etc.).
HttpPost属性表示该操作只能通过POST请求访问,它可以保护您免受其他请求类型(GET,PUT等)的影响。
POST
requests will also work without the attribute, but GET
requests will too! This might expose database queries which inserts, updates or removes data through GET requests, which is a bad practice. Imagine Google indexing a page like this: www.mysite.com/Users/Delete/{id}
, if you accept GET
requests, it might delete your complete user-base.
POST请求也可以在没有属性的情况下工作,但GET请求也是如此!这可能会暴露通过GET请求插入,更新或删除数据的数据库查询,这是一种不好的做法。想象一下谷歌索引这样一个页面:www.mysite.com/Users/Delete/{id},如果您接受GET请求,它可能会删除您的完整用户群。
GET
is to retrieve data, and POST
is to submit data. See this question for more info.
GET是检索数据,POST是提交数据。有关详细信息,请参阅此问题。
There are different ways to initiate a POST
request.
有不同的方法来发起POST请求。
You can wrap a form inside Html.BeginForm()
:
您可以在Html.BeginForm()中包装表单:
@using (Html.BeginForm())
{
@Html.LabelFor(m => m.UserName);
@Html.TextBoxFor(m => m.UserName);
@Html.LabelFor(m => m.Password);
@Html.PasswordFor(m => m.UserName);
<input type="submit" value="Login" />
}
Or via jQuery.post()
:
或者通过jQuery.post():
$.post(
'@Url.Action("MyMethod", "Home")',
{
// data for ClassA.
name: $('#username').val(); // example.
},
function (result) {
// handle the result.
});
But this GET
request won't work if you decorated your action with the HttpPost
attribute:
但是,如果使用HttpPost属性修饰操作,则此GET请求将不起作用:
$.get(
'@Url.Action("MyMethod", "Home")',
function (result) {
// this will not work.
});
Or if you try to access it through your browser. Also see this blogpost.
或者,如果您尝试通过浏览器访问它。另请参阅此博文。
#2
0
This method only accessed by POST.
此方法仅通过POST访问。
#1
1
The HttpPost
attribute indicates that the action can only be accessed through a POST
request, it protects you from other request types (GET, PUT etc.).
HttpPost属性表示该操作只能通过POST请求访问,它可以保护您免受其他请求类型(GET,PUT等)的影响。
POST
requests will also work without the attribute, but GET
requests will too! This might expose database queries which inserts, updates or removes data through GET requests, which is a bad practice. Imagine Google indexing a page like this: www.mysite.com/Users/Delete/{id}
, if you accept GET
requests, it might delete your complete user-base.
POST请求也可以在没有属性的情况下工作,但GET请求也是如此!这可能会暴露通过GET请求插入,更新或删除数据的数据库查询,这是一种不好的做法。想象一下谷歌索引这样一个页面:www.mysite.com/Users/Delete/{id},如果您接受GET请求,它可能会删除您的完整用户群。
GET
is to retrieve data, and POST
is to submit data. See this question for more info.
GET是检索数据,POST是提交数据。有关详细信息,请参阅此问题。
There are different ways to initiate a POST
request.
有不同的方法来发起POST请求。
You can wrap a form inside Html.BeginForm()
:
您可以在Html.BeginForm()中包装表单:
@using (Html.BeginForm())
{
@Html.LabelFor(m => m.UserName);
@Html.TextBoxFor(m => m.UserName);
@Html.LabelFor(m => m.Password);
@Html.PasswordFor(m => m.UserName);
<input type="submit" value="Login" />
}
Or via jQuery.post()
:
或者通过jQuery.post():
$.post(
'@Url.Action("MyMethod", "Home")',
{
// data for ClassA.
name: $('#username').val(); // example.
},
function (result) {
// handle the result.
});
But this GET
request won't work if you decorated your action with the HttpPost
attribute:
但是,如果使用HttpPost属性修饰操作,则此GET请求将不起作用:
$.get(
'@Url.Action("MyMethod", "Home")',
function (result) {
// this will not work.
});
Or if you try to access it through your browser. Also see this blogpost.
或者,如果您尝试通过浏览器访问它。另请参阅此博文。
#2
0
This method only accessed by POST.
此方法仅通过POST访问。