title: Handle both GET and POST request with same route in .net core controller
body: <p>I am trying to handle both the GET and the POST in the same controller with the same route, as I have certain rest calls that the data may use a GET or a POST to call the same endpoint....</p>
  <p>This works fine with a GET:</p>
  <pre><code>[Produces(&quot;application/json&quot;)]
  [ApiController]
  public class AccountController : ControllerBase
  {
      [HttpGet(&quot;GetAccount&quot;)]
      [Route(&quot;api/accounts/GetAccount&quot;)]
      public string GetAccount(string accountID)
      {
          return &quot;echoing accountID: &quot; + accountID;
      }
  }
  </code></pre>
  <p>And this works for POST:</p>
  <pre><code>[Produces(&quot;application/json&quot;)]
  [ApiController]
  public class AccountController : ControllerBase
  {
      [HttpPost(&quot;GetAccount&quot;)]
      [Route(&quot;api/accounts/GetAccount&quot;)]
      public string GetAccount([FromForm] string accountID)
      {
          return &quot;echoing accountID: &quot; + accountID;
      }
  }
  </code></pre>
  <p>But this does not return the values from a POST:</p>
  <pre><code>[Produces(&quot;application/json&quot;)]
  [ApiController]
  public class AccountController : ControllerBase
  {
      [HttpPost(&quot;GetAccount&quot;),HttpGet(&quot;GetAccount&quot;)]
      [Route(&quot;api/accounts/GetAccount&quot;)]
      public string GetAccount(string accountID)
      {
          // accountID is NULL when doing a POST, but is correct for a GET...
          return &quot;echoing accountID: &quot; + accountID;
      }
  }
  </code></pre>
  <p>In the above example, a GET request works fine, but when doing a POST, the parameter accountID is NULL, because I have removed the <code>[FromForm]</code> in order to make it work with a GET.</p>
  <p>Is there some way that I can combine this into a single route?</p>
  <p>This is for a .net core 5.0 site....</p>
  <p>Example of how I am posting to the endpoint from javascript:</p>
  <pre><code>$.ajax({
     url: '/api/accounts/GetAccount',
     data: {
        accountID: 'abcdefg'
     },
     type: 'POST',
     dataType: 'JSON',
     contentType: &quot;application/x-www-form-urlencoded; charset=UTF-8&quot; 
  })
     .done(function (result) {
        // verified that the result is NULL
        console.log(result);
     })
     .fail(function () {
        alert(&quot;ERROR&quot;);;
     })
     .always(function () {
        alert(&quot;DONE&quot;);
     });
  </code></pre>
  <p>And here is my complete Startup file (in case I don't have something registered correctly):</p>
  <pre><code>public class Startup
  {
     public Startup(IConfiguration configuration)
     {
        Configuration = configuration;
     }
  
     public IConfiguration Configuration { get; }
  
     public void ConfigureServices(IServiceCollection services)
     {
        services.AddSession();
        services.AddHttpContextAccessor();
        services.AddRazorPages();
        services.AddControllers();
        services.AddControllers().AddNewtonsoftJson();
        services.AddControllersWithViews();
     }
  
     public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
     {
        app.UseSession();
        app.UseExceptionHandler(&quot;/Error&quot;);
        app.UseHsts();
        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseRouting();
        app.UseAuthentication();
        app.UseAuthorization();
  
        app.UseEndpoints(endpoints =&gt;
        {
           endpoints.MapRazorPages();
           endpoints.MapControllers();
        });
      }
  }
  </code></pre>
  <p>Thanks!</p>