title: WEB API response message not shown if no data found?
body: <p>I try to work with api but the message is not shown when no data found.</p>
  <p>This is the controller code:</p>
  <pre class="lang-cs prettyprint-override"><code>public HttpResponseMessage GetPatResult(int Patid,int branchid)
  {
      using (DBEntities1 entities = new DBEntities1())
      {
          var entity = entities.LAB_RESULTS_CLINIC_VIEW.Where(e =&gt; e.Patient_No == Patid &amp;&amp; e.branchid==branchid).ToList();
          if (entity == null)
          {
              var message = string.Format(&quot;No Results Found &quot;);
              HttpError err = new HttpError(message);
              return Request.CreateResponse(HttpStatusCode.NotFound, err);
             
          }
          else
          {
              return Request.CreateResponse(HttpStatusCode.OK, entity);
             
          }
      }
  }
  </code></pre>
  <p>When data are found it shows the data, but when entity == null its not showing the message and shows only this row:</p>
  <blockquote>
  <p>This XML file does not appear to have any style information associated with it. The document tree is shown below.
  &lt;ArrayOfLAB_RESULTS_CLINIC_VIEW xmlns:i=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xmlns=&quot;http://schemas.datacontract.org/2004/07/aspnetWEBAPI&quot;/&gt;</p>
  </blockquote>
  <p>How to show message &quot;No Data Found&quot; if data not found?</p>
  <p>Also I updated the code from suggested answer link :</p>
  <p><a href="https://stackoverflow.com/questions/37492010/asp-net-mvc-api-returning-this-xml-file-does-not-appear-to-have-any-style-infor">asp.net mvc api returning &#39;This XML file does not appear to have any style information associated with it. The document tree is shown below.&#39;</a></p>
  <p>This is the WebApiConfig file :</p>
  <pre><code>public static void Register(HttpConfiguration config)
          {
                   // Web API routes
              config.MapHttpAttributeRoutes();
              config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue(&quot;text/html&quot;));
              var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t =&gt; t.MediaType == &quot;application/xml&quot;);
              config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
  
              config.Routes.MapHttpRoute(
                  name: &quot;DefaultApi&quot;,
                  routeTemplate: &quot;api/{controller}/{id}&quot;,
                  defaults: new { id = RouteParameter.Optional }
              );
        
          }
  </code></pre>
  <p>This is the Global.asax.cs</p>
  <pre><code>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;
          }
      }
  </code></pre>
  <p>After updated the code its now show [ ] when no data found ?</p>
  <p>UPDATE:</p>
  <p>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 ?</p>
  <p>This is the code of controller GetUsers:</p>
  <pre><code>public HttpResponseMessage GetUsers(string username, string password, int branchid)
          {
              using (DBEntities1 entities = new DBEntities1())
              {
                  var entityuser = entities.Users_web.Any(user =&gt; user.user_name.Equals(username, StringComparison.OrdinalIgnoreCase) &amp;&amp; user.user_password == password &amp;&amp; user.branch_id == branchid);
  
                  if (entityuser == true)
                  {
                      return Request.CreateResponse(HttpStatusCode.OK, entityuser);
                  }
                  else
                  {
                     
                      var message = string.Format(&quot;User Name or password not Correct &quot;);
                      HttpError err = new HttpError(message);
                      return Request.CreateResponse(HttpStatusCode.NotFound, err);
                  }
              }
  
          }
  </code></pre>
  <p>UPDATE 2 :</p>
  <p>I changed the code in controller GetPatResult to be same code in GetUsers :</p>
  <pre><code>var entity = entities.LAB_RESULTS_CLINIC_VIEW.Where(e =&gt; e.Patient_No == Patid &amp;&amp; e.branchid==branchid).ToList();
                  if (entity == null) {} 
  </code></pre>
  <p>to this code</p>
  <pre><code>var entity = entities.LAB_RESULTS_CLINIC_VIEW.Any(e =&gt; e.Patient_No == Patid &amp;&amp; e.branchid==branchid);
                  if (entity == true) {}
  </code></pre>
  <p>In this case when no data found message shows , but when data found its not show the data and show only true .</p>
  <p>How to fix this issue and show message if no data found and show the data if data found ?</p>