Posted on Leave a comment

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

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)
{
    ...
}
Posted on 1 Comment

Accessing ASP.NET Controls in JavaScript Code

ASP.NET controls essentially translate to a bunch of HTML and JavaScript code when being rendered in the browser, because HTML and JS is all what a browser can understand. So does that mean you can access an ASP.NET control with ease through JavaScript? Yes and no. Really depends on the version of .NET installed on the server (hosting your ASP.NET website).

HTML controls/elements/tags are generally referred to by their IDs. In plain JS, we would do something like:

var name = document.getElementById('txtName').value;

In jQuery, that would translate to:

var name = $('#txtName').val();

That’s assuming that your page has a text box with the ID ‘txtName’. One would expect that ASP.NET controls can also be referred to by their IDs in JavaScript. But that’s not always true. Prior to .NET 3, a compiler-generated prefix used to get added to a control’s ID (while rendering it in browser), to form what’s called the control’s ClientID. An example of such a ClientID can be “ctl00$mainpage$txtName”. In short, if you have an ASP.NET 3.0 textbox control having an ID ‘txtName’, you can access it in JavaScript like:

var name = document.getElementById('txtName').value;

But obviously, that won’t work in case of ASP.NET 2.0, because the actually ID you want to refer to is “ctl00$mainpage$txtName”.

That is where the ASP.NET short tag notation comes into play. Regardless of whatever version of .NET is installed on your server, you can access an ASP.NET control in JS in the following way:

var name = document.getElementById('<%= txtName.ClientId %>').value;

Of course, you can directly do something like:

var name = <%= txtName.Text %>;

Well, all depends on your needs. This tip is just meant to get you started with the interoperability between ASP.NET and JavaScript. And remember, the ASP.NET short tag notation works the same way in case of HTML too.

Further Reading
You can even have ASP.NET methods return data to your JavaScript code. That can be done with a little-bit of AJAX and by declaring your ASP.NET methods as webmethods. This beautiful tutorial discusses how to do just that (in plain JS and in jQuery)