Initialising Data seeding

Abdou/Fawzy Aboyoussef 20 Reputation points
2024-11-20T01:04:33.73+00:00

I am following Rick Anderson's lectures about ASP.Net Core Mvc. I am facing a problem in the fifth part about working with a database, concerning data seeding.
In program.cs I find a problem with the following statement:
//-----------------------
app.MapStaticAssets();
//-------------------------
When running the app I get the following error:

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?)
//-------------------------------
Rick has written: Test the app. Force the app to initialize, calling the code in the Program.cs file, so the seed method runs. To force initialization, close the command prompt window that Visual Studio opened, and restart by pressing Ctrl+F5.
//------------------------------
I tried everything, but not good enough because I still get the same error.
Does the problem relate to the seed method?
Appreciating any help

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,670 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,556 Reputation points Microsoft Vendor
    2024-11-20T03:13:20.4333333+00:00

    Hi @Abdou/Fawzy Aboyoussef

    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:

    1. Use the latest version Visual Studio and install the .NET 9.0 Runtime (via Visual Studio Installer) enter image description here
    2. Check the project file, make sure you are using the .net9.0 TargetFramework, and upgrade the related package to 9.0 version. enter image description here
    3. Open the project root folder, delete the bin and obj folder, and then rebuild the application.

    Besides, when using MapStaticAssets, refer to the following code:

    enter image description here

    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.AspNetCore.Identity;
    using Microsoft.EntityFrameworkCore;
    using Net9MVCSample.Data;
    
    var builder = WebApplication.CreateBuilder(args);
    
    // Add services to the container.
    var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
    builder.Services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(connectionString));
    builder.Services.AddDatabaseDeveloperPageExceptionFilter();
    
    builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
        .AddEntityFrameworkStores<ApplicationDbContext>();
    builder.Services.AddControllersWithViews();
    
    var app = builder.Build();
    
    // Configure the HTTP request pipeline.
    if (app.Environment.IsDevelopment())
    {
        app.UseMigrationsEndPoint();
    }
    else
    {
        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.UseRouting();
    
    app.UseAuthorization();
    
    app.MapStaticAssets();
    
    app.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}")
        .WithStaticAssets();
    
    app.MapRazorPages()
       .WithStaticAssets();
    
    app.Run();
    
    

    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

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.