palapapa
❔ Is it a good idea to use a "processed model" in addition to the model bound by ASP.NET itself?
First of all, I wanted to do this because I wanted to avoid writing a custom model binder.
Let's say I want to get a string from the query string in a request, and convert that string into
DotNet.Glob
(which requires calling Glob.Parse(string)
). If I don't want to convert it manually in the action method, the other option is to write a custom model binder, but I don't want to do that because it seems like a hassel and the documentation isn't good about how to properly implement one. So I came up with the following constuct:
Then in my action method, I can just do:
This seems to work fine. Is this a good idea instead of a custom model binder?4 replies
❔ What is the purpose of `ModelBindingContext.Model`, `ModelName`, and `ModelBinderAttribute.Name`?
The docs are very unclear about this. If I should set
ModelBindingContext.Result
to ModelBindingResult.Success(result)
, then what is the point of setting ModelBindingContext.Model
?
Also, what is the point of ModelBindingContext.ModelName
? I don't understand what its value is supposed to be from the docs alone. From my understanding, the "model" being referred to here is my custom model class, then how am I supposed to "look up values in IValueProvider
during model binding," if the model is the thing I need to set? This also brings it to my last question, which is what exactly is ModelBinderAttribute.Name
setting?3 replies
❔ Inconsistency in the documentation of model binding for complex types?
In https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-7.0#complex-types, it says:
For example, with a query containing ?Instructor.Id=100&Name=foo, bound to method OnGet(Instructor instructor), the resulting object of type Instructor contains:But later it also says:Id
set to100
.
For binding to a parameter, the prefix is the parameter name.Don't these two contradict each other? If the second sentense were true, then
Id
wouldn't have been bound because the prefix should have been instructor
not Instructor
.8 replies
❔ What's the difference between `[Bind]` and `[BindNever]` when applied to a class?
I have read https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-7.0#attributes-for-complex-type-targets, and their difference seems to be that
Bind
can be applied at just the class and specify what properties to bind, while BindNever
is just like the inverse of Bind
except that it needs to be applied to the properties that you don't want to bind. It seems like they differ only in the places where they should be applied. Is this correct?2 replies
✅ Should I implement `IEqualityOperators` for a record?
It makes me wonder why the compiler doesn't automatically implement
IEqualityOperators
for all record types even though it already automatically overloads operator==
and operator!=
for all record types.4 replies
❔ Why doesn't every method in Collection<T> has an extension point?
For example,
Insert
calls InsertItem
, and InsertItem
by default calls items.Insert(index, item);
(source), where items
is the IList<T>
instance passed into its constructor. InsertItem
is also virtual. This means that to customize Collection<T>.Insert
, you can either override InsertItem
, or pass an IList<T>
instance into the constructor of Collection<T>
that has the custom behavior you want.
But for methods like Contains
, there isn't an extension point like, for example, ContainsItem
. That means the only way to customize Contains
is to pass an IList<T>
instance into the constructor of Collection<T>
that has the custom behavior you want.
This raises the question of why there are two ways to customize methods like Insert
, Clear
, Remove
, and the setter of the indexer(SetItem
), while there is only one for the other methods, like the getter of the indexer and Contains
. Is there any specific reason for this?4 replies
❔ Same URL pages holding different states
This isn't really a asp.net problem, but I discovered this while reading its documentation and don't know where to ask this weird question.
To know what I am asking about, open this URL https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-7.0&tabs=visual-studio in two tabs, then in one of the tabs, on the nav bar on the left, go to "APIs > Controller-based APIs > Tutorials > Create a web API with controllers" and open it. You will notice that now the two tabs have the exact same URL, but the navbars in the two tabs show different things. This persists even through refreshes. How can two tabs with the exact same URL show different things even after refreshing both of them?
9 replies
❔ What's the difference between `IConfiguration.Get` and `IConfiguration.Bind`?
The docs say
Get
"Attempts to bind the configuration instance to a new instance of type T. If this configuration section has a value, that will be used. Otherwise binding by matching property names against configuration keys recursively." And Bind
"Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively." The description of IConfigurationSection.Value
is only "Gets or sets the section value."
What does IConfigurationSection.Value
mean and what's the difference between IConfiguration.Get
and IConfiguration.Bind
?54 replies
❔ What does the "required" constraint do in route templates?
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-7.0#route-templates says the "required" constraint "Used to enforce that a non-parameter value is present during URL generation." I don't know what that means and can't seem to find any information about it.
5 replies
✅ Why is the trailing dot of a route template optional?
Here https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-7.0#route-templates it mentions that "If only a value for filename exists in the URL, the route matches because the trailing . is optional." But just before that it says "Literal text other than route parameters (for example, {id}) and the path separator / must match the text in the URL. "
12 replies
❔ What is the difference between .cshtml files and .razor files?
I couldn't find a definitive answer about this anywhere. They both use the razor syntax. Are they interchangeable? Are there features that are only available in .cshtml files and not in .razor files and vice versa?
17 replies
✅ How does the implementation of `IServiceProvider.GetServices` work?
In https://source.dot.net/#Microsoft.Extensions.DependencyInjection.Abstractions/ServiceProviderServiceExtensions.cs,ed6b36f4cd78b748, you can see that it only calls
GetRequiredService
with IEnumerable<T>
, which then calls GetService
. There is nothing in the contract of IServiceProvider
that says if you pass an IEnumerable<T>
as the service type to GetService
, it would return a IEnumerable
of all registered services. Does this mean that GetServices
only works with the internal implementation of IServiceProvider
by MS?1 replies
Understanding await control flow [Answered]
According to my understanding, when the control flow reaches an
await
, it would return immediately to the call site. But what would happen if the method you are currently awaiting awaits another method?
Let's say I have 3 methods: Main
, AsyncMethod1
, and AsyncMethod2
. If I call AsyncMethod1
in Main
, the control flow would immediately return to Main
as far as I know. Then, inside AsyncMethod1
, it awaits AsyncMethod2
. In that case, would the control flow return to AsyncMethod1
? But that wouldn't make much sense because AsyncMethod1
is already awaiting something so it cannot continue. Which part of my understanding is wrong?
By the way, if I have an async Main
, where would the control flow return to when I await in Main
? Does it simply not return?37 replies