faisal1208 faisal1208 - 23 days ago 13
MySQL Question

The format of value '\r\n' is invalid.","ExceptionType":"System.FormatException

I have created an API in visual studio 2015 using

MySQL
Database. I have two methods of GET. One that gets all records and second that get records by ID. The first method is working perfect but the second one raises an exception.

{"Message":"An error has occurred.","ExceptionMessage":"The format of value '\r\n' is invalid.","ExceptionType":"System.FormatException","StackTrace":" at System.Net.Http.Headers.MediaTypeHeaderValue.CheckMediaTypeFormat(String mediaType, String parameterName)\r\n at System.Net.Http.Headers.MediaTypeHeaderValue..ctor(String mediaType)\r\n at System.Net.Http.HttpRequestMessageExtensions.CreateResponse[T](HttpRequestMessage request, HttpStatusCode statusCode, T value, String mediaType)\r\n at WebServiceMySQL.Controllers.MetersInfoController.Get(Int32 id) in E:\\Work\\NEW API\\WebServiceMySQL\\WebServiceMySQL\\Controllers\\MetersInfoController.cs:line 33"}


Below is my code

Class

public partial class meters_info_dev
{
public int id { get; set; }
public string meter_msn { get; set; }
public string meter_kwh { get; set; }
}


Controller

public MeterEntities mEntities = new MeterEntities();

// GET all
public HttpResponseMessage Get()
{
try
{
return Request.CreateResponse(HttpStatusCode.Found, mEntities.meters_info_dev.ToList());
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
}
}

// GET by ID
public HttpResponseMessage Get(int id)
{
try
{
return Request.CreateResponse(HttpStatusCode.Found, mEntities.meters_info_dev.SingleOrDefault(m => m.id == id), Environment.NewLine);
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
}
}


WebApiConfig

// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
config.Formatters.Remove(config.Formatters.XmlFormatter);

// Web API routes
config.MapHttpAttributeRoutes();


config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);


While debugging the
Get(int id)
part I found a query which is

SELECT Extent1.id, Extent1.meter_msn, Extent1.meter_kwh FROM meters_info_dev AS Extent1


And running this query I found the following result

enter image description here

I don't know what's the real problem and I am stuck to it.

Any help would be highly appreciated.

Answer Source

You are passing this Environment.NewLine as last parameter of CreateResponse() and NewLine is expanded to string '\r\n'. Last parameter should be configuration, media type or formatter or not passed at all. Few lines above you are using it with two params and it works. Check documentation here