Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8713071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:15:21+00:00 2026-06-13T05:15:21+00:00

Background I am coding an API layer in C# using MVC 4 Web API

  • 0

Background

I am coding an API layer in C# using MVC 4 Web API Project (ASP.NET).

I have written a test action in my controller like this:

[System.Web.Mvc.HttpPost]
public string TestAction(FormCollection fc)
{
    return "test";
}

I am using Poster in FireFox to test a form post:

  • content-type is set to application/x-www-form-urlencoded
  • Body from parameters is set to foo=bar&bar=foo

Here is my route:

RouteTable.Routes.MapHttpRoute("PostDefault", "{controller}/{action}");

Question

I am able to hit the controller with no problem when posting a url and parameters from Poster, but if I put a breakpoint on return "test"; and then hover over the FormCollection that is passed in (fc) It shows that an empty array of strings was passed in rather than my values (e.g. string[0]).

I have done this loads of times using jQuery $.post() on the client with no problems receiving the form collection.

What is it failing in this case?

Thanks,

Matt

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:15:22+00:00Added an answer on June 13, 2026 at 5:15 am

    It seems that this is an action defined inside an API controller (ApiController). If this is the case you cannot use FormCollection. This is used for normal controller actions, not API controllers (personally I don’t use it even in normal controller actions but that’s another topic, see the next paragraph to understand what I am using instead).

    You should define a view model:

    public class MyViewModel
    {
        public string Foo { get; set; }
        public string Bar { get; set; }
    }
    

    and then have your controller action take this view model as parameter:

    public class TestController: ApiController
    {
        [System.Web.Http.HttpPost]
        public string TestAction(MyViewModel model)
        {
            return "test";
        }
    }
    

    Notice that the correct attribute for an API controller is [System.Web.Http.HttpPost] and not [System.Web.Mvc.HttpPost] as in your code.

    Also I would recommend you sticking to RESTful conventions:

    RouteTable.Routes.MapHttpRoute("PostDefault", "{controller}");
    

    and then:

    public class TestController: ApiController
    {
        public string Post(MyViewModel model)
        {
            return "test";
        }
    }
    

    Now the HTTP Verb (POST in this case) determines the controller action to be invoked:

    POST /test
    Server: http://example.com
    Content-Type: application/x-www-form-urlencoded
    Content-Length: 15
    Connection: close
    
    foo=bar&bar=foo
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am coding an ASP.NET MVC 3 app. When a user logs in I
I'm coming from the .Net MVC background looking to do a project in RoR.
I have a VB.net project which uses a background worker to do some stuff.
Hi I'm very new to web development but have a coding background. I'm trying
I like a dark background when coding and have customised it to a scheme
Just to give a little background: I'm using Dreamweaver CS5 for coding php, XAMPP
I have a pretty good non-OOP background. I've done lots of Visual Basic coding,
Coding in UNIX make , I have written: cat $ sString | grep sSubstring
Background info: I'm coding with C#, using Microsoft SQL Server for databases. I didn't
I'm pretty new to coding and I am currently using a background image for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.