I am passing JSON object back to a C# aspx page web method.
My JavaScript looks like:
return jsonItem = {
id: id,
title: title,
registrationPage: {
registration: registration
}
}
I am passing this item to an aspx web method:
$.ajax({
type: "POST",
url: "SaveXML.aspx/SaveAs",
data: JSON.stringify({ jsonItem : jsonItem }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg, textStatus) {
alert(textStatus);
},
});
The JSON payload is being sent fine.
In the aspx page I have the classes:
public class JSONItem
{
public string Id { get; set; }
public string Title { get; set; }
public RegistrationPage RegistrationPage { get; set; }
}
public class RegistrationPage
{
public string Registration { get; set; }
}
[WebMethod]
public static Policy Save(Policy policy)
{
}
When I try building this I get a code analysis error, which makes sense:
CA1034 : Microsoft.Design : Do not nest type ‘SaveXML.RegistrationPage’. Alternatively, change its accessibility so that it is not externally visible.
If I ignore the error, the code does work. I did try changing the modifiers but then the Registration Page object wasn’t populated.
How do I get the code to work and comply with the above error?
Thanks
By the localisation of your WebMethod it looks like you have nested
JSONItemandRegistrationPageinside yourPageclass. To be exact your code looks like this:You should move those classes out of
Pageclass (they can remain in same file of course):