Can I register a custom connectivity device besides Global.asax?

It would be convenient to limit the scope of a custom binding object only to a specific method of action of the controller or its entire controller. Hanselman wrote a sentence that implied alternative places for user registration of pattern binding, but did not seem to end:

You can either put this custom custom extension for all your DateTimes by registering it in Global.asax

Is it possible to make these registrations in a smaller controller system? If so, is there any reason to avoid this outside of Global.asax MvcApplication (e.g. performance considerations)?

+4
source share
1 answer

When I closed the tabs that I opened for this question, which I did not get to the ModelBinderAttribute , I found someone with the answer , You can assign ModelBinderAttribute your view models:

 [ModelBinder(typeof(SomeEditorModelModelBinder))] public class SomeEditorModel { // display model goes here } public class SomeEditorModelModelBinder : DefaultModelBinder { // custom model binder for said model goes here } 

Although this was not quite what I was looking for, it is even more specific than registering it for a controller or controller method.

Update

Thanks to Levy's remark indicating a much better solution. If you consume an object using a custom binder in the MVC action method directly, you can simply decorate this method parameter with the ModelBinder property.

 public ActionResult SomeMethod([ModelBinder(typeof(SomeEditorModelBinder))]SomeEditorModel model) { ... } 
+5
source

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


All Articles