后与同样的方法得到签名 [英] post and get with same method signature

查看:128
本文介绍了后与同样的方法得到签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的控制器我有两个动作称为朋友。一个执行一个取决于它是否是一个得与后。

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".

所以,我的code段是这个样子:

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();
}

不过,这并不能编译,因为我有两种方法具有相同签名(友)。我该如何去创造呢?我是否需要创建只是一个动作,而是get和后区分它里面?如果是这样,我该怎么做呢?

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?

推荐答案

第二种方法重命名为别的东西,如Friends_Post,然后你可以添加 [ActionName(老友记)] 属性到第二个。因此,请求给朋友行动POST作为请求类型,将通过行动来处理。

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.

// 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();
}

这篇关于后与同样的方法得到签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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