• 请不要在回答技术问题时复制粘贴 AI 生成的内容
a199261800
V2EX  ›  程序员

.net 里面要怎么做才能把数据验证做在 web api 请求之前,类似于 filter 那种效果

  •  
  •   a199261800 · Apr 12, 2017 · 2118 views
    This topic created in 3356 days ago, the information mentioned may be changed or developed.

    刚好有这个需求,不知道能不能实现

    5 replies    2017-04-12 22:41:06 +08:00
    owt5008137
        1
    owt5008137  
       Apr 12, 2017 via Android
    什么叫做“做在 web api 之前”?是指发送 http 请求之前?那和.net 有毛关系?

    如果是在 http 请求之前并且是想找微软给你集成好的数据验证,你可以看下这个 https://docs.microsoft.com/en-us/aspnet/ajax/cdn/jquery-validate/
    leeg810312
        3
    leeg810312  
       Apr 12, 2017 via Android
    编写一个继承 ActionFilterAttribute 的类,重写 OnActionExecuting ,验证数据,通过则执行 base.OnActionExecuting ,否则返回一个包含错误信息的 Response
    beginor
        4
    beginor  
       Apr 12, 2017 via Android
    如果用 owin 跑 webapi 的话,可以写一个中间件 middleware ,放在 webapi 之前
    manhere
        5
    manhere  
       Apr 12, 2017
    middleware
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2510 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 47ms · UTC 11:17 · PVG 19:17 · LAX 04:17 · JFK 07:17
    ♥ Do have faith in what you're doing.