Reputation: 822
I try to work with api but the message is not shown when no data found.
This is the controller code:
public HttpResponseMessage GetPatResult(int Patid,int branchid)
{
using (DBEntities1 entities = new DBEntities1())
{
var entity = entities.LAB_RESULTS_CLINIC_VIEW.Where(e => e.Patient_No == Patid && e.branchid==branchid).ToList();
if (entity == null)
{
var message = string.Format("No Results Found ");
HttpError err = new HttpError(message);
return Request.CreateResponse(HttpStatusCode.NotFound, err);
}
else
{
return Request.CreateResponse(HttpStatusCode.OK, entity);
}
}
}
When data are found it shows the data, but when entity == null its not showing the message and shows only this row:
This XML file does not appear to have any style information associated with it. The document tree is shown below. <ArrayOfLAB_RESULTS_CLINIC_VIEW xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/aspnetWEBAPI"/>
How to show message "No Data Found" if data not found?
Also I updated the code from suggested answer link :
This is the WebApiConfig file :
public static void Register(HttpConfiguration config)
{
// Web API routes
config.MapHttpAttributeRoutes();
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("text/html"));
var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
This is the Global.asax.cs
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
}
}
After updated the code its now show [ ] when no data found ?
UPDATE:
I have another controller GetUsers when entityuser == false its showing the message 'User Name or password not Correct' the different between 2 controllers variable entityuser in GetUsers deal as boolean and variable entity in GetPatResult deal as string may be this will affect boolean VS string ?
This is the code of controller GetUsers:
public HttpResponseMessage GetUsers(string username, string password, int branchid)
{
using (DBEntities1 entities = new DBEntities1())
{
var entityuser = entities.Users_web.Any(user => user.user_name.Equals(username, StringComparison.OrdinalIgnoreCase) && user.user_password == password && user.branch_id == branchid);
if (entityuser == true)
{
return Request.CreateResponse(HttpStatusCode.OK, entityuser);
}
else
{
var message = string.Format("User Name or password not Correct ");
HttpError err = new HttpError(message);
return Request.CreateResponse(HttpStatusCode.NotFound, err);
}
}
}
UPDATE 2 :
I changed the code in controller GetPatResult to be same code in GetUsers :
var entity = entities.LAB_RESULTS_CLINIC_VIEW.Where(e => e.Patient_No == Patid && e.branchid==branchid).ToList();
if (entity == null) {}
to this code
var entity = entities.LAB_RESULTS_CLINIC_VIEW.Any(e => e.Patient_No == Patid && e.branchid==branchid);
if (entity == true) {}
In this case when no data found message shows , but when data found its not show the data and show only true .
How to fix this issue and show message if no data found and show the data if data found ?
Upvotes: 1
Views: 1743
Reputation: 204
Your "entity" variable is not entity really but a List<Entity>. When no entities are found, it is not null but empty list, and you can check it with (!entity.Any())
var entity = entities.LAB_RESULTS_CLINIC_VIEW
.Where(e => e.Patient_No == Patid && e.branchid==branchid)
.ToList();
if (!entity.Any())
{
var message = string.Format("No Results Found ");
HttpError err = new HttpError(message);
return Request.CreateResponse(HttpStatusCode.NotFound, err);
}
If you are going to get single entity or null then call not .ToList() but .FirstOrDefault()
var entity = entities.LAB_RESULTS_CLINIC_VIEW
.Where(e => e.Patient_No == Patid && e.branchid==branchid)
.FirstOrDefault();
if (entity == null)
{
var message = string.Format("No Results Found ");
HttpError err = new HttpError(message);
return Request.CreateResponse(HttpStatusCode.NotFound, err);
}
Upvotes: 2
Reputation: 43949
Try to change
var message = string.Format("No Results Found ");
HttpError err = new HttpError(message);
return Request.CreateResponse(HttpStatusCode.NotFound, err);
To this
return Request.CreateResponse(HttpStatusCode.NotFound, "No Results Found");
or better to use modern IHttpActionResult:
public IHttpActionResult GetPatResult(int Patid,int branchid)
{
using (DBEntities1 entities = new DBEntities1())
{
var entity = entities.LAB_RESULTS_CLINIC_VIEW.Where(e => e.Patient_No == Patid && e.branchid==branchid).ToList();
if (entity == null)
{
return NotFound("No Results Found ");
}
else
{
return Ok( entity);
}
}
}
Upvotes: 0