Click here to Skip to main content
14,932,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Or for instance,
[FromForm]string values
.
I want to write my own, [FromSomething].

What I have tried:

Searched on the net, but could not find any information how to do that.
Posted
Updated 18-Feb-21 11:27am

You are seeing the result of the Decorator pattern (ability to add those attributes). So you have to read a bit about that pattern.
Try this article which explains it more in depth: Adding decorated classes to the ASP.NET Core DI container using Scrutor[^]
   
Comments
BillWoodruff 2-Dec-20 17:11pm
   
+5
csrss 2-Dec-20 17:14pm
   
I dont really see how this can help
The ASP.NET Core source code is available on Git Hub. You can look at the existing attributes to see how they are implemented.

For example:
aspnetcore/FromQueryAttribute.cs at master · dotnet/aspnetcore · GitHub[^]

The attributes seem to use the BindingSource class:
aspnetcore/BindingSource.cs at master · dotnet/aspnetcore · GitHub[^]

Which then seems to be used by various types derived from BindingSourceValueProvider:
aspnetcore/BindingSourceValueProvider.cs at master · dotnet/aspnetcore · GitHub[^]

Eg: QueryStringValueProvider[^], which is created by QueryStringValueProviderFactory[^], which is called from MvcCoreMvcOptionsSetup[^], which is called from MvcCoreServiceCollectionExtensions[^]

The setup is quite complicated, so you'll need to follow the rabbit-hole to find all of the bits you need.
   
Comments
csrss 3-Dec-20 6:10am
   
So far, this is the most relevant answer. However, I did that already, and though that there is an easy way, like with model binding. I'll probably stick to my current solution. Thanks anyway
I googled "c# custom attribute" and the first result appears to have the answer, Creating Custom Attributes (C#) | Microsoft Docs[^]
   
Comments
csrss 2-Dec-20 14:22pm
   
But this is something completely different
BabyYoda 2-Dec-20 14:31pm
   
How so?
csrss 2-Dec-20 14:34pm
   
But even if it is what I am looking for, how can I get HttpContext, how can I set output parameters, etc - there is no information in this link you posted. I dont see, how can I extract data from HttpRequest and pass it up as a Controller parameter.
BabyYoda 2-Dec-20 14:37pm
   
I think you need to be more clear as to what you want to do. Your title is about how to create custom attributes. The document I linked to explains how.

Looking closer at your question it looks like you don't want custom attributes but what you actually might want is model binding. This might have something https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-5.0.

If that does not help then I suggest you edit your question and make it more clear what you are wanting to do.
csrss 2-Dec-20 14:45pm
   
The question is correct I think, because [FromBody] is an attribute, but it is some sort of built-in attribute. I am already doing it like it is show on the link you provided, like this:
[ModelBinder(typeof(MyModelBinder))] MyClass myClass
But it feels that it can be done better, something like this:
[FromRequest]MyClass myClass
And this is what I am asking about, how to do it.
BabyYoda 2-Dec-20 15:07pm
   
And what would your custom binding be? And why?
csrss 2-Dec-20 15:09pm
   
Anything, extraction of user role, auth token, any sort of information, required to run an endpoint.
BabyYoda 2-Dec-20 15:16pm
   
Is it for a controller method or for any method in a regular class?
csrss 2-Dec-20 15:16pm
   
Only for controller method
BabyYoda 2-Dec-20 15:24pm
   
I still think an example would be helpful. Auth Token should work with FromQuery or FromBody. How else would an auth token be posted to your method?

A user role should be retrieved using your own business logic, not posted to your method.

I think you may want to take a different route. Or at least I still don't see what you could add that isn't already there.
csrss 2-Dec-20 15:26pm
   
Well, I am not asking for an advice what route should I take, right?
Maybe creating such attributes is not possible and there are just built-ins.
BabyYoda 2-Dec-20 15:32pm
   
I don't follow what you need but I do believe what you are asking for is custom model binding. https://docs.microsoft.com/en-us/aspnet/core/mvc/advanced/custom-model-binding?view=aspnetcore-5.0
Dave Kreskowiak 2-Dec-20 15:33pm
   
There are two sides to an attribute. There is the code that defines the attribute and which code objects it can be attached to, then there is the code you have to write that looks for the attribute on objects and does whatever it appropriate based on the attribute and its properties.
I know this is 1Y old question, but actually not answered

Parameter Binding in ASP.NET Web API - ASP.NET 4.x | Microsoft Docs[^]

This url explain how to do that, actually you need to inherit from HttpParameterBinding attribute, and implement GetBinding method. Right fromurl and frombody are implemented in another way, but asp.net core provide this way to extend/implement the same officially.

Check this section
Parameter Binding in ASP.NET Web API - ASP.NET 4.x | Microsoft Docs[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900