Controller Attribute Check like using [FromBody] twice

可紊 提交于 2020-01-06 09:06:08

问题


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.

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.

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?

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

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?


回答1:


In a way, there are runtime dependencies; namely the content of any request that might be routed to the action in question. You can perform automated integration tests by emulating the server and client generating a well formed request and confirm an expected result as outlined here:

https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-2.1




回答2:


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.

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);
}


来源:https://stackoverflow.com/questions/50695691/controller-attribute-check-like-using-frombody-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!