asp.net-mvc 使用相同的方法签名发布和获取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/724386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:28:23  来源:igfitidea点击:

post and get with same method signature

asp.net-mvcpostcontrollergetasp-net-mvc-1

提问by codette

In my controller I have two actions called "Friends". The one that executes depends on whether or not it's a "get" versus a "post".

在我的控制器中,我有两个名为“朋友”的操作。执行的那个取决于它是“get”还是“post”。

So my code snippets look something like this:

所以我的代码片段看起来像这样:

// Get:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Friends()
{
    // do some stuff
    return View();
}

// Post:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Friends()
{
    // do some stuff
    return View();
}

However, this does not compile since I have two methods with the same signature (Friends). How do I go about creating this? Do I need to create just one action but differentiate between a "get" and "post" inside of it? If so, how do I do that?

但是,这不会编译,因为我有两个具有相同签名(朋友)的方法。我该如何去创造这个?我是否只需要创建一个动作,但在其中区分“获取”和“发布”?如果是这样,我该怎么做?

回答by ?a?da? Tekin

Rename the second method to something else like "Friends_Post" and then you can add [ActionName("Friends")]attribute to the second one. So the requests to the Friend action with POST as request type, will be handled by that action.

将第二个方法重命名为“Friends_Post”之类的其他名称,然后您可以向第二个方法添加[ActionName("Friends")]属性。因此,请求类型为 POST 的 Friend 操作的请求将由该操作处理。

// Get:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Friends()
{
    // do some stuff
    return View();
}

// Post:
[ActionName("Friends")]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Friends_Post()
{
    // do some stuff
    return View();
}

回答by mohrtan

If you truly only want one routine to handle both verbs, try this:

如果你真的只想要一个例程来处理这两个动词,试试这个:

[AcceptVerbs("Get", "Post")]
public ActionResult ActionName(string param1, ...)
{
//Fun stuff goes here.
}

One potential caveat: I'm using MVC release 2. Not sure if this was supported in MVC 1. The Intellisense documentation for AcceptVerbs should let you know.

一个潜在的警告:我正在使用 MVC 版本 2。不确定 MVC 1 中是否支持这一点。AcceptVerbs 的 Intellisense 文档应该让您知道。

回答by KateA

Try using:

尝试使用:

[AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)]
public ActionResult Friends()
{
    // do some stuff
    return View();
}

回答by Ariel

not entirely sure if it is the correct way, but i would use a meaningless parameter to differentiate the sigs. like:

不完全确定这是否是正确的方法,但我会使用一个无意义的参数来区分信号。喜欢:

// Get:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Friends(bool isGet)
{
    // do some stuff
    return View();
}

// Post:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Friends()
{
    // do some stuff
    return View();
}

I know it's ugly and hackish, but it works.

我知道这很丑陋,但它有效。

回答by codette

Marking cagdas' response as the answer since it answered my question. However, since I don't like using the ActionName attribute in my project I use a different solution. I simply added the FormCollection to the "post" action (which ends up changing the method signature)

将 cagdas 的回答标记为答案,因为它回答了我的问题。但是,由于我不喜欢在我的项目中使用 ActionName 属性,因此我使用了不同的解决方案。我只是将 FormCollection 添加到“post”操作(最终更改了方法签名)

// Get:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Friends()
{
    // do some stuff
    return View();
}

// Post:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Friends(FormCollection form)
{
    // do some stuff
    return View();
}

回答by Matt Kocaj

add to the Post method the params to want to receive in the post. maybe like this:

将要在帖子中接收的参数添加到 Post 方法。也许是这样的:

// Post:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Friends(string friendName, string otherField)
{
    // do some stuff
    return View();
}

..or if you have a complex type, like this:

..或者如果你有一个复杂的类型,像这样:

// Post:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Friends(Friend friend)
{
    // do some stuff
    return View();
}

Edit:It would be preferable to use a more typed-approach to receiving the posted items, like above.

编辑:最好使用更多类型的方法来接收发布的项目,如上。

回答by Adrian Grigore

Your action methods can't be doing the same thing, otherwise there would be no need to to write two action methods. So if the semantics are different, why not use different names for the action methods as well?

你的动作方法不能做同样的事情,否则就没有必要写两个动作方法了。那么如果语义不同,为什么不为动作方法也使用不同的名称呢?

For example, if you had a "delete" action method and GET just asks for confirmation, you might call the GET method "ConfirmDelete" and the POST method just "Delete".

例如,如果您有一个“删除”操作方法,而 GET 只要求确认,您可能会调用 GET 方法“ConfirmDelete”而 POST 方法只是“删除”。

Not sure if that matches your scenario, but it always did for me when I had the same problem.

不确定这是否符合您的情况,但是当我遇到同样的问题时,它总是对我有用。