How to map a 1 or 0 in an ASP.Net MVC route segment into a Boolean action method input parameter

后端 未结 4 1594
无人共我
无人共我 2021-01-12 01:25

We have some PHP and Javascript apps that call into some ASP.NET MVC endpoints. Let\'s say we have this endpoint:

public ActionResult DoSomething(bool flag)
         


        
4条回答
  •  逝去的感伤
    2021-01-12 02:16

    You could use a custom model binder, as found in this SO question ASP.NET MVC controller actions with custom parameter conversion?

    Now if you wanted to do this globally so you didn't have to set an attribute on every bool that might get a 0/1 instead of true/false, then you can extend the default model binder to understand 0 or 1 as false or true by overriding the default model binder, as found in this SO question Change the default model binder in asp.net MVC

    Some combination of these two should get you what you want.

提交回复
热议问题