CusvalController.cs
*********************
// GET api/cusval
string constring = string.Empty;
// public DataTable Get()
public DataTable Get()
{
constring = ConfigurationManager.ConnectionStrings["sqlconnectstring"].ConnectionString.ToString();
DataTable dt = new DataTable();
NameValueCollection nvc = HttpUtility.ParseQueryString(Request.RequestUri.Query);
var u = nvc["uname"];
var p = nvc["pwd"];
var sqlstring = nvc["sql"];
SqlCommand cmd = new SqlCommand("SELECT * FROM login where uname='" + username + "' and pwd='" + pwd + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
return dt;
}
so, when i try to get JSON response, first time i got error. so, i used below coding in Register()... in global.ascx.cs then it worked
global.ascx.cs
****************
namespace API
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class WebApiApplication : System.Web.HttpApplication
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}",
defaults: new { id = RouteParameter.Optional }
);
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
}
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
Now i got the JSON respone on browser like below:
[
- {
uname:"christhu",
place:"chennai",
status:"good"
}
]
so, the problem is. i am not able to do parsing as it does not have root key name. just need to put one common name for that.i mean i need to get response like below insteated of old respone.
[
- Result: {
uname:"christhu",
place:"chennai",
status:"good"
}
]
help needed.