0

I came across an issue when coding methods in a controller that have separate Get and Post executions. Normally when we scaffold a controller against a model, the Get and Post methods have different parameter definitions because the Post method is normally capturing data from the form to save to the model/database.

But I was just coding an original set of Get/Post methods where the parameter list was actually the same. I can later post my example code to show this, but that might be distracting from my question; i.e., we can acknowledge there's always another way to code any given method. In this case, I'd rather get an answer to this question.

If the Get and Post methods have the same parameter definition, then that's a syntax error. So I needed a way around this problem. The following was my logic.

Since I can't change the name of the method (that's an assumption on my part that may not be valid), the only way around this is to change the parameter definition of one or the other of the Get/Post variations. So I added an additional parameter to the Get method and the compiler was happy. I don't actually supply that parameter when invoking the method, and of course I don't use the parameter in the method, but it does give the method a different parameter definition and it solved my dilemma.

But I wanted to ask if everyone else handles this coding issue different ways so I might learn a better way to do it. It may be that my assumption about method naming is incorrect as well.

Added after original post: (@CodeCaster, lesson learned). I removed the internal code as it's not related:

Function ChannelTopicSelector(ByVal ChannelType As ChannelType, ByVal Topic As String) As ActionResult
  Return View()
End Function

<HttpPost()>
<ValidateAntiForgeryToken()>
Function ChannelTopicSelector(ByVal ChannelType As ChannelType, ByVal Topic As String) As ActionResult
  Return View()
End Function

The first method above returns the 'multiple definitions with identical signatures' error.

I followed CodeCaster's link which I believe is also Brad's point and I understand how the ActionName Attribute is applied. I hadn't noticed this before as my coding never got into yet.

Regards the duplicate question, I did look for other posts, I just didn't know how to frame the question correctly for it to come up in the search. Should this post be deleted then? Anyway, my question is answered - thx

Alan
  • 1,587
  • 3
  • 23
  • 43
  • 1
    When you say you can't change the name of the method, is that just because the URL must stay the same? If so, then you can use the `ActionName` attribute. – DavidG Feb 04 '15 at 15:58
  • http://stackoverflow.com/q/6536559/11683 – GSerg Feb 04 '15 at 15:59
  • Or [GET and POST methods with the same Action name in the same Controller](http://stackoverflow.com/questions/9552761/get-and-post-methods-with-the-same-action-name-in-the-same-controller). – CodeCaster Feb 04 '15 at 15:59
  • And I disagree with your premise; code often illustrates a problem more proper than a wall of text. – CodeCaster Feb 04 '15 at 16:00
  • Will add code from now on... – Alan Feb 04 '15 at 18:04

1 Answers1

2

You can use the ActionNameAttribute and just name the method differently. e.g.

VB.NET

<ActionName("Foo")> _
Public Function Foo_GET(id As Int32) As ActionResult
End Function

<ActionName("Foo"), HttpPost> _
Public Function Foo_POST(id As Int32) As ActionResult
End Function

C#

[ActionName("Foo")]
public ActionResult Foo_GET(Int32 id)
{
}

[ActionName("Foo"), HttpPost]
public ActionResult Foo_POST(Int32 id)
{
}
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • Your response example was also helpful because I hadn't known you can put multiple attributes within the '[...]', or '<...>' for me in VB. – Alan Feb 04 '15 at 18:07
  • Glad to hear. And sorry about that, missed the VB tag. I'll post the VB.NET equivalent as well. – Brad Christie Feb 04 '15 at 18:57