The attribute before the method has many usages,they can provide a way of associating information with code in a declarative way. They can also provide a reusable element that can be applied to a variety of targets.
What they mean depends on your method and what you want to do
For example,[AcceptVerbs("GET", "POST")] can make the method support both get and post.
[AcceptVerbs("GET", "POST")]
public IActionResult VerifyEmail([RegularExpression("^[a-zA-Z0-9_\\.-]+@([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$")] string email)
{
if (!ModelState.IsValid)
return Json($"Don't use {email},change a form right now!");
else
return Json(true);
}
or it can be used to get the parmeter from the models.
[HttpGet("{id}")]
public ActionResult<Pet> GetById(int id, bool dogsOnly)
#endregion
{
var pet = _petsInMemoryStore.FirstOrDefault(
p => p.Id == id &&
(!dogsOnly || p.PetType == PetType.Dog));
if (pet == null)
{
return NotFound();
}
return pet;
}
<div class="panel panel-default">
<div class="panel-heading">@Title</div>
<div class="panel-body">@ChildContent</div>
<button class="btn btn-primary" @onclick="OnClick">
Trigger a Parent component method
</button>
</div>
@code {
public string Title { get; set; }
public RenderFragment ChildContent { get; set; }
public EventCallback<MouseEventArgs> OnClick { get; set; }
}
If you do not add [Parameter] attribute, those are just public properties that can't be set from other pages.That is to say the following line would be invalid :
<ChildComponent Title="Panel Title from Parent" />
The correct way should be:
<div class="panel panel-default">
<div class="panel-heading">@Title</div>
<div class="panel-body">@ChildContent</div>
<button class="btn btn-primary" @onclick="OnClickCallback">
Trigger a Parent component method
</button>
</div>
@code {
[Parameter]
public string Title { get; set; }
[Parameter]
public RenderFragment ChildContent { get; set; }
[Parameter]
public EventCallback<MouseEventArgs> OnClickCallback { get; set; }
}
Then it allows you could set the parameters whenever we use that component:
<ChildComponent Title="Panel Title from Parent"
OnClickCallback="@ShowMessage">
Content of the child component is supplied
by the parent component.
</ChildComponent>
Best Regards,
Rena
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
I don't know where you use this parameter, if you use this parameter in blazor it means binding routing parameter or other in-page data, [parameter] is essential.
Member
5 Points
2 Posts
what does [parameter] before method means?
May 29, 2020 03:35 AM|behzad.sdghi|LINK
what does [parameter] before method means?
Member
10 Points
13 Posts
Re: what does [parameter] before method means?
May 29, 2020 05:15 AM|Tsaisoul|LINK
The attribute before the method has many usages,they can provide a way of associating information with code in a declarative way. They can also provide a reusable element that can be applied to a variety of targets.
What they mean depends on your method and what you want to do
For example,[AcceptVerbs("GET", "POST")] can make the method support both get and post.
or it can be used to get the parmeter from the models.
you can search the official tutorial , they are useful,https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/attributes
Contributor
2690 Points
874 Posts
Re: what does [parameter] before method means?
May 29, 2020 05:45 AM|Rena Ni|LINK
Hi behzad.sdghi,
Did you use blazor?
[Parameter]
is used to mark the component parameters that can be set when the component is used in another page.Reference:
https://docs.microsoft.com/en-us/aspnet/core/blazor/components?view=aspnetcore-3.1#component-parameters
For more detailed exlpanation,in this code below:
If you do not add
[Parameter]
attribute, those are just public properties that can't be set from other pages.That is to say the following line would be invalid :The correct way should be:
Then it allows you could set the parameters whenever we use that component:
Best Regards,
Rena
Participant
1620 Points
927 Posts
Re: what does [parameter] before method means?
May 29, 2020 05:45 AM|PaulTheSmith|LINK
If, when you say [parameter] you actually mean [Parameter]
and when you say 'method' you mean 'property'
then this page may help https://docs.microsoft.com/en-us/aspnet/core/blazor/components?view=aspnetcore-3.1&viewFallbackFrom=aspnetcore-3.0#component-parameters
If my assumptions are not correct can you show the code where this attribute is used?'
Member
80 Points
32 Posts
Re: what does [parameter] before method means?
May 29, 2020 05:47 AM|Evern|LINK
Hi,
I don't know where you use this parameter, if you use this parameter in blazor it means binding routing parameter or other in-page data, [parameter] is essential.
Regards,
Evern