Click here to Skip to main content
15,881,709 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[^]
 
Share this answer
 
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.
 
Share this answer
 
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 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[^]
 
Share this answer
 
I googled "c# custom attribute" and the first result appears to have the answer, Creating Custom Attributes (C#) | Microsoft Docs[^]
 
Share this answer
 
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.

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