【问题标题】:The format of value '\r\n' is invalid.","ExceptionType":"System.FormatException值 '\r\n' 的格式无效。","ExceptionType":"System.FormatException
【发布时间】:2017-09-26 11:28:02
【问题描述】:

我在 Visual Studio 2015 中使用 MySQL 数据库创建了一个 API。我有两种 GET 方法。一个获取所有记录,第二个通过 ID 获取记录。第一种方法运行良好,但第二种方法引发了异常。

{"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"}

下面是我的代码

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

控制器

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 }
        );

在调试Get(int id) 部分时,我发现了一个查询

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

运行这个查询我发现了以下结果

我不知道真正的问题是什么,我被困住了。

任何帮助将不胜感激。

【问题讨论】:

  • @EpicKip 其实代码里有个Environment.NewLine,也是出错的地方。

标签: c# mysql linq rest asp.net-web-api2


【解决方案1】:

您将此Environment.NewLine 作为CreateResponse() 的最后一个参数传递,并且NewLine 扩展为字符串'\r\n'。最后一个参数应该是配置、媒体类型或格式化程序,或者根本不传递。您上面的几行代码将它与两个参数一起使用,并且可以正常工作。查看文档here

【讨论】:

    猜你喜欢
    • 2019-11-04
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 2016-09-20
    • 2011-03-30
    • 1970-01-01
    • 2019-12-15
    相关资源
    最近更新 更多