ASP.NET Web API allows only one body parameter in POST

Posted on Leave a comment

Say with me, “ASP.NET Web API allows only one body parameter in POST”. And again, “ASP.NET Web API allows only one body parameter in POST”. Now repeat it 100 times.

It’s tiresome and probably utterly unnecessary. But that’s the only way to remember it. Believe me. I keep falling in the trap and repeating the same mistake over and over. I have wasted countless hours on this simple, stupid thing.

Don’t be like me. Memorize this mantra. Do it now. Do not delay it.

You know my pain if you have ever received this super-generic message and spent a gazillion fertile hours googling for a solution.

No action was found on the controller that matches the request

It’s like being told that your car cannot be driven because a part is missing. But, wait! Which part? Which f**k**g part? If you have been through this pain, you are my friend.

There could be countless reasons for this error. Perhaps you tried to access the wrong URL. Perhaps you used the wrong HTTP method. Perhaps you misspelled a parameter name.

Or, if you are like me, perhaps you had more than one parameter in a POST API method. Like this:

public async Task<HttpResponseMessage> AddProduct(int productId, string productName)
{
     ...
}

You were probably anticipating multiple values for an entity called Product in your HTTP POST request. So you created your API method like this in a bout of natural instinct. You were not wrong. ASP.NET is stupid. Because it allows for only one POST body parameter, it forces you to create a separate class to handle multiple parameters.

Do this to fix your woes:

public class Product
{
    int productId { get; set; };
    string productName { get; set; }
}
 
public async Task<HttpResponseMessage> AddProduct(Product prod)
{
    ...
}

Mobile App Development: Lessons Learned

Posted on 1 Comment
  1. Sencha Touch is a great framework, but requires a LOT getting used to. The officials Docs do not always have the answer you’re looking for. ST forums and stackoverflow are excellent resources to consult when in need.
  2. If you are a web developer, DO NOT waste time learning Objective-C or Java for creating native iOS and Android apps. Instead use something like ST to develop a mobile web app, and then convert it into a native app using Cordova / PhoneGap.
  3. Cordova is the renamed, open-source version of PhoneGap.
  4. If your app is data-centeric, most probably it will depend on a webservice / API. If the API and the app are hosted on the same server, no problemo. In case of native apps, that are basically web apps PhoneGapped into native apps, that’d mean calling a remote API, and that is the problem. See same origin policy.
  5. Most googled solutions will point to making the service JSONP supported; but JSONP works only for GET requests. CORS is a recent W3C standard that supports all HTTP methods, but it still doesn’t work for PhoneGapped apps. ASP.NET Web API provides an easy CORS implementation.
  6. The perfect solution is to keep making Ajax calls normally, but using the full URL of the remote API. That will work because a PhoneGapped app doesn’t render in a browser but in a WebView (through a file:// URL). So it’s not restricted by browser’s same origin policy.
  7. ASP.NET MVC 5 and Web API are awesome!
  8. You may frequently encounter annoying cache issues with PhoneGapped apps. Just place a super.clearCache() in your Android app’s main activity’s onCreate().
  9. A PhoneGapped iOS app will run in fullscreen mode, by default, such that the status bar in iOS 7+ will appear over it. A fix is right here!
  10. One can create an IPA archive for testing on iOS devices via Build > *.app > iTunes > ?*.ipa. Believe me, it’s one of the most stupid things you will ever do. This is the correct way to create IPA archives for ad hoc distribution.
  11. Here’s how to create an animated splash screen in Android (though I have yet to figure out how to correctly use this in a ST-PhoneGapped app).
  12. If your device has Android 4.4+, you can remote debug your WebView-based Android apps using Chrome.
  13. JavaScript is yummy!