Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Some random type
- public class RandomModel
- {
- public int Integer { get; set; }
- public string String { get; set; }
- }
- // Action request model with various binding sources
- public class BindingModel
- {
- [FromBody]
- public RandomModel Body { get; set; }
- [FromForm(Name = "MyField")]
- public string Form { get; set; }
- [FromQuery(Name = "MyQuery")]
- public string Query { get; set; }
- [FromRoute(Name = "id")]
- public int Route { get; set; }
- [FromHeader(Name = "MyHeader")]
- public string Header { get; set; }
- }
- // Controller with POST action binding our model and a dependency injected service
- public class MyController : Controller
- {
- [HttpPost]
- public IActionResult Action(BindingModel model, [FromServices]IUrlHelperFactory urlHelper)
- {
- return View();
- }
- }
- // Test - PASSES!!!
- [Fact]
- public void UltimateSuperDuperCrazyModelBindingTest()
- {
- MyMvc
- .Routes()
- .ShouldMap(request => request
- .WithLocation("/My/Action/100?myQuery=Test")
- .WithMethod(HttpMethod.Post)
- .WithJsonBody(new { Integer = 1, String = "MyBodyValue" })
- .WithFormField("MyField", "MyFieldValue")
- .WithHeader("MyHeader", "MyHeaderValue"))
- .To<MyController>(c => c.Action(
- new BindingModel
- {
- Body = new RandomModel { Integer = 1, String = "MyBodyValue" },
- Form = "MyFieldValue",
- Route = 100,
- Query = "Test",
- Header = "MyHeaderValue"
- },
- From.Services<IUrlHelperFactory>()));
- }
- // P.S. Do not write such actions! :)
Advertisement
Add Comment
Please, Sign In to add comment