-
Notifications
You must be signed in to change notification settings - Fork 10k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update webapp template to use top-level statements & minimal hosting API #34016
Update webapp template to use top-level statements & minimal hosting API #34016
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
@SteveSandersonMS / @javiercn in case you'd like to chime in on the formatting / shape of the code. |
LGTM 👍🏿 |
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(
builder.Configuration.GetConnectionString("DefaultConnection"))); Is it more legible if the connection string is stored in a variable? var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlite(connectionString)); |
@pranavkm I'm happy to make that change. @davidfowl has some pretty strong opinions on this and did not like my original proposal that declared variables for |
Note that when the global implicit namespaces change lands the namespaces in this using Microsoft.AspNetCore.Identity; // Needed for the IdentityUser type
using Microsoft.EntityFrameworkCore; // Needed for the UseSqlite extension method
using webapp.Data; // Needed for the ApplicationDbContext type |
I guess let's keep with this for now and we can always update it once @davidfowl's had a chance to weigh in on the change. |
- Using minimal hosting APIs - Using top-level statements
- using minimal hosting APIs - using top-level statements
@JamesNK in case you'd like to chime in on the gRPC project template changes. |
- uses minimal hosting APIs - uses top-level statements
- uses minimal hosting APIs - uses top-level statements
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gRPC template 👍
- Uses minimal hosting APIs - Uses top-level statements - Fixed template testing scripts to support BlazorWasm template properly - Fixed nullable issues
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As long as it works!
There's a super subtle difference to where UseRouting is called implicitly with the WebApplicationBuilder that we should make sure is ok. Routing will run before static files in a couple of these templates. Maybe we want to change that back so it's more explicit in those scenarios? |
@davidfowl that's a good point but I must say I love the fact we can omit it. Is the difference big enough to warrant being explicit you think? Is the concern about performance, behavior, correctness, ease of diagnosing failure modes, etc.? |
I guess to 2 differences are:
These can be worked around obviously by adding an explicit call but maybe we should measure. |
Doesn't routing run before static files today? Matching at least will. |
This is also the case today, isn't it? The I believe the StaticFiles middleware avoids doing work if it detects there's an endpoint set on the request, so routes should already win? |
No it doesn't. For example here is the Razor pages template: public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
} Things that run before routing:
That's not the problem the "problem" is that it runs route matching first, which is a change from today. It may be irrelevant but we should be aware of the ordering differences here. |
I'm not sure what you mean. If you explicitly call MapFallback? Sure but I'm not talking about that scenario. |
src/ProjectTemplates/Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Program.cs
Outdated
Show resolved
Hide resolved
src/ProjectTemplates/Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Program.cs
Outdated
Show resolved
Hide resolved
src/ProjectTemplates/Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Program.cs
Show resolved
Hide resolved
src/ProjectTemplates/Web.ProjectTemplates/content/StarterWeb-CSharp/Program.cs
Outdated
Show resolved
Hide resolved
|
||
builder.Services.AddControllersWithViews(options => | ||
{ | ||
var policy = new AuthorizationPolicyBuilder() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that looks strange. Seems like a relic?
Not gonna lie these templates are impossible to review because of those ifs. You really want to look at the resulting content for each of them. Those could be baseline files checked into the repo or something. |
@davidfowl certain baselines are generated in the |
- put connection string in its own variable - add explicit call to UseRouting() to avoid issue with middleware that re-runs the pipeline with a different path (e.g. UseExceptionHandler)
I've added calls to @pranavkm @davidfowl I did the Planning to merge once checks finish on this latest commit. |
Updated project templates to use top-level statements and minimal hosting APIs.
Here's what
Program.cs
ends up looking like with individual auth. (Note that most of theusing
statements will get removed when the implicit global usings SDK PR is merged):Contributes to #33947 #33948 #33944 #32471 #33813