コード例 #1
0
        /// <summary>
        /// Handles POST requests that create new entities in the entity set.
        /// </summary>
        /// <param name="entity">The entity to insert into the entity set.</param>
        /// <returns>The response message to send back to the client.</returns>
        public virtual HttpResponseMessage Post([FromBody] TEntity entity)
        {
            TEntity createdEntity = CreateEntity(entity);
            TKey    entityKey     = GetKey(entity);

            return(EntitySetControllerHelpers.PostResponse(this, createdEntity, entityKey));
        }
コード例 #2
0
        public void PostResponse_Throws_IfODataPathMissing()
        {
            ApiController controller = new Mock <ApiController>().Object;
            var           request    = new HttpRequestMessage(HttpMethod.Post, "http://localhost/Customers");

            controller.Configuration = new HttpConfiguration();
            controller.Request       = request;

            Assert.Throws <InvalidOperationException>(
                () => EntitySetControllerHelpers.PostResponse <Customer, int>(controller, new Customer(), 5),
                "A Location header could not be generated because the request does not have an associated OData path.");
        }
コード例 #3
0
        public void PostResponse_Throws_IfODataPathDoesNotStartWithEntitySet()
        {
            ApiController controller = new Mock <ApiController>().Object;
            var           request    = new HttpRequestMessage(HttpMethod.Post, "http://localhost/Customers");

            controller.Configuration = new HttpConfiguration();
            request.SetODataPath(new ODataPath(new MetadataPathSegment()));
            controller.Request = request;

            Assert.Throws <InvalidOperationException>(
                () => EntitySetControllerHelpers.PostResponse <Customer, int>(controller, new Customer(), 5),
                "A Location header could not be generated because the request's OData path does not start with an entity set path segment.");
        }
コード例 #4
0
        /// <summary>
        /// Handles POST requests that create new entities in the entity set.
        /// </summary>
        /// <param name="entity">The entity to insert into the entity set.</param>
        /// <returns>A <see cref="Task"/> that contains the response message to send back to the client when it completes.</returns>
        public virtual async Task <HttpResponseMessage> Post([FromBody] TEntity entity)
        {
            TEntity createdEntity = await CreateEntityAsync(entity);

            return(EntitySetControllerHelpers.PostResponse <TEntity, TKey>(this, createdEntity, GetKey(createdEntity)));
        }