使用 HTTP 操作动词的 ASP.Net MVC 路由
我正在编写一个实现 ASP.NET Web 应用程序,当我调用特定的 url 时,我想根据请求是 GET 还是 POST 来调用不同的操作方法。
我尝试了两种不同的方法 - 首先,我从具有以下操作方法的控制器开始:
<HttpGet>
Public Function Index() as ActionResult
...
End Function
<HttpPost>
Public Function Index() as ActionResult
...
End Function
...然后注册一个路由,将 .action 设置为 Url 的“Index”。这无法编译,因为这两个函数具有相同的签名。
接下来,我尝试将 Post 例程的名称更改为“Subscribe”,但这当然需要我向路由表提供两条不同的路由(以便指定新的操作方法名称)。如果我这样做,我会发现第二个被第一个抵消了。
我现在正在考虑回到原来的想法,但为其提供一个虚拟参数,并将其指定为 UrlParameter.Optional。通过将其作为后例程中的参数,这将创建一个新的方法签名,我希望这是可以的。
不过,它闻起来有点肮脏的黑客味道 - 所以我想知道其他人会怎么做?
马丁.
诗。我现在已经尝试了肮脏的黑客,它确实有效。但仍然有兴趣听取其他人的意见。
I am writing a implementing a ASP.NET web app, and when I call against a particular url, I want to call a different action method depending on whether the request is a GET or a POST.
I've tried two different approaches - first of all I start with a controller with the following action methods:
<HttpGet>
Public Function Index() as ActionResult
...
End Function
<HttpPost>
Public Function Index() as ActionResult
...
End Function
... and then registering a Route which sets .action to "Index" for the Url. This doesn't compile, because the two functions have the same signature.
Next I tried changing the name of the Post routine to "Subscribe", but of course that would require me to supply two different routes to the routing table (so as to specify the new action method name). If I do that, I find that the second one is cancelled out by the first.
I'm now thinking of going back to my original idea, but supplying a dummy parameter to it, and specifying it as UrlParameter.Optional. By having this as an argument in the post routine, this will create a new method signature, which I would expect would be ok.
It smells a bit of a dirty hack though - so I was wondering how other people would go about it?
Martin.
Ps. I have now tried the dirty hack, and it does work. Still interested in hearing the views of others though.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
重命名操作并使用
ActionNameAttribute
但我不知道您的 POST 方法会做什么,因为它没有获取任何数据。 POST 操作通常会获取一些要处理的后期数据,因此可能还需要一些参数。当您添加它们时,您的两个方法可以共享相同的名称,因为它们的签名不同(一种不带参数,一种带参数)。
Rename actions and use
ActionNameAttribute
But I don't know what your POST method will do because it doesn't get any data. POST actions usually get some post data to process, so it would probably need some parameters as well. When you'd add them your two methods could share the same name since their signatures would differ (one would be without parameters, and one with them).
只是一个简短的注释来强调我最终采取的实际方法 - 尽管罗伯特上面的答案也完全有效......
我最终在 MapRoute 函数的约束参数上使用了“.httpMethodConstraint”属性,然后提供了单独的方法所涉及的各种方法类型的路线。
我认为,罗伯特的解决方案通常是一种更好的方法 - 但对于我的具体问题,采用映射不同路线的方法是我的技术主管的首选解决方案。
其中没有太多内容 - 除非我在一个大网站上工作,否则我会对创建大量路由持保留态度,因为它们很快就会变得混乱。
欢呼帮助...
Just a short note to highlight the actual approach I took in the end - although Robert's answer above is perfectly valid as well...
I ended up using a ".httpMethodConstraint" attribute on the constraint argument of the MapRoute function, and then provided separate routes for the various method types involved.
Robert's solution is, I think, generally a better approach to take - but for my specific problem taking an approach of mapping distinct routes is the preferred solution of my tech lead.
There's not a lot in it - except if I were working on a big site I would have reservations about creating loads and loads of Routes, as they can get confusing quickly.
Cheers for the help...