本文介绍了我怎么可以重载基于接收到的HTTP动词ASP.NET MVC操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想用一个GET / PUT相同的URL /删除/ POST一个基于REST API,但是当对操作唯一不同的是HTTP动词它接受,就认为它们是重复的!

Wanted to use the same URL for a GET/PUT/DELETE/POST for a REST based API, but when the only thing different about the Actions is which HTTP verbs it accepts, it considers them to be duplicate!

类型已经定义了一个名为索引具有相同的参数类型的成员。

"Type already defines a member called 'Index' with the same parameter types."

要我说,那又怎么样?这一次只接受GET,这个人只接受POST ......应该能成为共存吧?

To which I said, so what? This one only accepts GET, this one only accepts POST... should be able to be co-exist right?

如何?

推荐答案

这不是ASP.NET MVC限制或什么的。这是.NET和类是如何工作的:不管你怎么努力,你不能在同一类的两种方法具有相同的名称而采取相同的参数。你可以欺骗使用属性:

That's not ASP.NET MVC limitation or whatever. It's .NET and how classes work: no matter how hard you try, you cannot have two methods with the same name on the same class which take the same parameters. You could cheat using the [ActionName] attribute:

[HttpGet]
[ActionName("Foo")]
public ActionResult GetMe()
{
   ...
}

[HttpPut]
[ActionName("Foo")]
public ActionResult PutMe()
{
   ...
}

[HttpDelete]
[ActionName("Foo")]
public ActionResult DeleteMe()
{
   ...
}

[HttpPost]
[ActionName("Foo")]
public ActionResult PostMe()
{
   ...
}

当然在真正的RESTful应用程序的不同的动词会采取不同的参数一样,所以你很少有这样的情况。

Of course in a real RESTFul application the different verbs would take different parameters as well, so you will seldom have such situations.

您可以看看有关您的路线如何组织的一些想法。

You may take a look at SimplyRestful for some ideas about how your routes could be organized.

这篇关于我怎么可以重载基于接收到的HTTP动词ASP.NET MVC操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 20:00