How .net MVC parses arguments

I recently posted a new version of my Advanced Controller

This is basically a Generic Controller (ASHX), which acts on ASP.net web formats as an MVC controller, in the sense that it receives a request and automatically calls the required internal method, parses its arguments from a querystring or params request depending on HTTP -verb and returns the result. It is much more, but it was the basic initial functionality.

Creating an object and hydrating from the request information was the most painful part of the project, but in the end I achieved the same performance as the same request made with the MVC.

As I said, .net MVC controllers actually do the same thing, but I never managed to find out how to do this.

For example, if I make this AJAX call in an MVC controller:

$.ajax({ type:'GET', url: 'SomeData/List' data:{filter: 'whatever'} }); 

This will call the controller method, which will receive the String filter property.

But it can become more complex, since we can call controllers that receive complex types, with complex types and collections nested and the โ€œmagicallyโ€ arguments appear correctly parsed into the controller arguments.

Does anyone know how this plumbing works in .net MVC?

+4
source share
1 answer

This process is called model binding. There, the Binder model is used by default, which will try to convert the published values โ€‹โ€‹to a complex type by looking at what is in the request and what the controller expects.

This is done based on conventions. Obviously, views also implement this convention, so you donโ€™t have friction when using views and controllers with the Binder Model.

The following posts explain the concept in more detail:

http://odetocode.com/blogs/scott/archive/2009/04/27/6-tips-for-asp-net-mvc-model-binding.aspx http://odetocode.com/blogs/scott/archive /2009/05/05/iterating-on-an-asp-net-mvc-model-binder.aspx http://www.singingeels.com/Articles/Model_Binders_in_ASPNET_MVC.aspx

If you use Google to โ€œbind the ASP.NET MVC model,โ€ you will find much more information on this.

+4
source

Source: https://habr.com/ru/post/1483313/


All Articles