如何在Web Api中将日期作为参数传递

时间:2015-12-30 22:39:23

标签: c# entity-framework linq asp.net-web-api linq-to-sql

我目前想知道如何将日期值传递给我的Web Api?

控制器中我的代码片段:

/// Example: /api/firearms/olderthan/2015-12-30
        [Route("olderthan/{olderthan}")]
        public IQueryable<FirearmDto> GetFirearmsByDateolderthan(string olderthan)
        {
            DateTime temp = Convert.ToDateTime(olderthan);
            return db.Firearms
                .Where(f => f.LastUpdate <= temp)
                .Select(AsFirearmDto);
        }

我收到以下错误,如下所示:

<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace/>
<InnerException>
<Message>An error has occurred.</Message>
<ExceptionMessage>
The cast to value type 'System.Int32' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace>
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal) at lambda_method(Closure , Shaper ) at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper) at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext() at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext() at WriteArrayOfFirearmDtoToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , CollectionDataContract ) at System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.DataContractSerializer.WriteObject(XmlWriter writer, Object graph) at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content) at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__1b.MoveNext()
</StackTrace>
</InnerException>
</Error>

对我错过/做错的任何帮助或见解将不胜感激。 感谢。

2 个答案:

答案 0 :(得分:0)

我认为您的数据格式不是xml格式。如果此格式为json,则您的内容类型必须为application / json。请检查您的内容类型并将其更改为application / json。我认为它有效。

答案 1 :(得分:0)

我对我的代码进行了此更改,现在可以使用了。谢谢你们。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="shop_attributes">
  <tbody>
    <tr class="">
      <th>Reserved</th>
      <td>
        <p>false</p>
      </td>
    </tr>
    <tr class="alt">
      <th>Show Terms And Conditions</th>
      <td>
        <p>true</p>
      </td>
    </tr>
    </td>
    </tr>
  </tbody>
</table>