A set of technologies in the .NET Framework for building web applications and XML web services.
Error (active) CS1061 'WebApplication' does not contain a definition for 'MapStaticAssets' and no accessible extension method 'MapStaticAssets' accepting a first argument of type 'WebApplication' could be found (are you missing a using directive or an assembly reference?)
For this error, it does not relate the seed method. It might relate that you didn't add the.NET 9 SDK and Runtime in your computer.
So, you can check and try the following:
- Use the latest version Visual Studio and install the .NET 9.0 Runtime (via Visual Studio Installer)
- Check the project file, make sure you are using the
.net9.0TargetFramework, and upgrade the related package to 9.0 version. - Open the project root folder, delete the bin and obj folder, and then re-build the application.
Finally, if still not working, try to create a new .NET 9 MVC application and check whether it works or not.
In my application, the Program.cs file like this:
using Microsoft.EntityFrameworkCore;
using WebApplication1.Models;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<MvcMovieContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("MvcMovieContext") ?? throw new InvalidOperationException("Connection string 'MvcMovieContext' not found.")));
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
//Seed data
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
SeedData.Initialize(services);
}
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/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.MapStaticAssets(); //add MapStaticAssets()
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}")
.WithStaticAssets();
app.Run();
More information about the Seed Data, you can check the tutorial: work with a database in an ASP.NET Core MVC app.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion