控制器属性检查,例如两次使用[FromBody] [英] Controller Attribute Check like using [FromBody] twice

查看:60
本文介绍了控制器属性检查,例如两次使用[FromBody]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET MVC中(不管版本如何,如果重要的话,假设我使用的是最新的Core 2.1),我们通过使用属性(例如 [HttpPost]] 表示应被称为 POST 的方法,而 [FromQuery] 则应被称为来自查询字符串的方法参数.

In ASP.NET MVC (regardless of version, if it's important, assume I am using the latest Core 2.1) we control the behavior and parameter binding of the app by annotating the controller with attributes, for example [HttpPost] for a method that is supposed to be called as POST and [FromQuery] for a method parameter that is supposed to come from the query string.

现在,正如我在最近几个月中发现的艰难方法一样,有无数方式可以将其混淆.例如,如果您有两个参数声明为 [FromBody] ,则一个参数始终为 null ,因为只有一个参数可以表示主体.或者,如果您的方法被标记为 [HttpGet] ,则您的 [FromBody] 将返回 null ,因为该标准指出,这样做不是这样.相信我,还有更多的方法可以弄错它.

Now as I have found out the hard way in the last months, there are countless ways to mix this up. If you have two parameters declared as [FromBody] for example, one will always be null because only one can represent the body. Or if your method is tagged with [HttpGet] your [FromBody] will come back null because the standard says that's not how it's done. And believe me, there are many more ways to get it wrong.

我接受这些都是我的错误.如果我正确地做到了,它将起作用.但是,令我感到困惑的是,这是可以在编译时 找到的.在此,运行时依赖项为零.而且我只在调试时才发现它不起作用.我知道在我的程序中查找逻辑缺陷不是编译器的工作,但可以肯定的是,控制器工厂在构造逻辑缺陷时会发现它们吗?单元测试中,我将所有控制器类型都放入了测试方法中吗?还是诸如静态代码分析或stylecop之类的工具?

I accept that these are all my mistakes. If I do it correctly, it will work. However, what baffles me is that this is something that could be found at compile time. There is zero runtime dependencies in this. And I only find out when I debug it and it doesn't work. I understand that it's not the compilers job to find logical flaws in my program, but surely they could be found by a controller factory upon constructing it? A unit test where I throw all my controller types into a test method? Or a tool like static code analysis or stylecop?

我用Google搜索,然后得出了零.因此,我的假设是我的Google技术无法胜任.

I googled and I came up with zero. So my assumption is that my google skill is not up to it.

.NET Framework中真的没有方法可以检查我所有的控制器属性是否加起来可以正确提供服务?没有办法告诉我我...把它蒙住了吗?

Is there really no method in the .NET Framework to check if all my controller attributes add up to something that can be served correctly? Is there no way to tell me I fu...mbled that up?

如果真的没有,在我自己写之前,是有原因的吗?如果我自己写这篇文章,是否有任何已知的原因为什么不应该这样做?

And if there really is none, before I write it myself, is that for a reason? If I wrote this myself is there any known reason why one shouldn't do that?

推荐答案

事实证明,没有什么可以做我需要的,但是没有什么可以阻止您自己做.

As it turns out, no there is nothing out there doing what I needed, but there is nothing stopping you from doing it yourself.

我写了一个漂亮的小库类,可以在单元测试中将控制器提供给我,它会给我带来一系列错误,然后使测试失败.

I wrote a nice little library class that I can feed my controllers to in a unit test and it will give me a stream of errors that will then make the test fail.

由我编写的类代码不是我的,而是我的老板,所以我不能在这里发布它,但这没什么特别的,只是对公共方法及其参数进行了大量的反射检查属性,以确保他们匹配.所以是的,有可能没有什么特别的,如果您像我一样需要它,那就继续吧,用通用方法 ForController为自己编写一个整洁的静态小类 CheckCorrectUsageOfAttributes 并编写如下测试:

The code to the class, while written by me, is not mine, but my employers, so I cannot post it here, but it is nothing special, just a lot of reflection checking attributes on public methods and their parameters making sure they match. So yes, it's possible, there is nothing special to it, if you need it as much as I do, just go ahead and write yourself a neat little static class CheckCorrectUsageOfAttributes with a generic method ForController and write tests like this:

/// <summary>
/// Tests the account controller.
/// </summary>
[TestMethod]
public void TestAccountController()
{
    // arrange

    // act
    var result = CheckCorrectUsageOfAttributes.ForController<AccountController>();

    // assert
    var first = result.FirstOrDefault();
    Assert.IsNull(first, first);
}

这篇关于控制器属性检查,例如两次使用[FromBody]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆