示例#1
0
        public IHttpActionResult UnprocessableEntity_Action(PersonViewModel_POST model)
        {
            if (!ModelState.IsValid)
            {
                // Send a 422 response
                return(this.UnprocessableEntity());
            }

            // Return
            return(this.Ok());
        }
        public IHttpActionResult UnprocessableEntity_Action(PersonViewModel_POST model)
        {
            if (!ModelState.IsValid)
            {
                // Send a 422 response
                return this.UnprocessableEntity();
            }

            // Return
            return this.Ok();
        }
        public void ValidationError_Returns422_Always()
        {
            // Arrange
            var viewmodel = new PersonViewModel_POST()
            {
                FirstName = null,
                LastName = "Smith",
                EmailAddress = "www.google.com"
            };

            var config = new HttpConfiguration();
            config.Routes.MapHttpRoute("Default", "{controller}/{action}");

            var server = new HttpServer(config);

            using (var client = new HttpMessageInvoker(server))
            {
                // Act
                using (var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/Test/UnprocessableEntity_Action")
                {
                    Content = new ObjectContent(typeof(PersonViewModel_POST), viewmodel, new JsonMediaTypeFormatter())
                })
                using (var response = client.SendAsync(request, CancellationToken.None).Result)
                {
                    // Assert
                    Assert.IsNotNull(response);
                    Assert.AreEqual(response.ReasonPhrase, "Unprocessable Entity");
                    Assert.AreEqual(response.StatusCode, (HttpStatusCode)422);
                }
            }
        }