ASP.NET – self contained – missing method exception or styles and scripts not found 404

I made some changes to a razor webapp that still uses asp.net-core-tag-helpers (which is deprecated but according to the docs can still be used). The biggest change is the switch to .net9.0. Locally the application runs fine. But if i run

dotnet publish -p:TargetFramework=net9.0 -p:Nullable=enable 
   -p:Version=1.0.4.0 -p:AssemblyVersion=1.0.4 -p:FileVersion=1.0.4.0 
   --self-contained --runtime win-x64
# i also tried
dotnet publish -c release --self-contained --runtime win-x64

And start the myrazor_webapp.exe under windows 10 i get

Unhandled exception. System.MissingMethodException: 
     Method not found: 
'Microsoft.AspNetCore.Builder.RouteHandlerBuilder 
 Microsoft.AspNetCore.Builder.EndpointRouteBuilderExtensions.MapGet(
 Microsoft.AspNetCore.Routing.IEndpointRouteBuilder, System.String, System.Delegate)'.
     at Program.<Main>$(String[] args)

This is the code in my program.cs

var builder = WebApplication.CreateBuilder(args);

// Logging
builder.Logging.ClearProviders();
builder.Logging.AddConsole(); 
builder.Logging.AddDebug();

// 2024-12-02
builder.Services.AddScoped<UserService>();

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddControllers(); 

// Entity Framework 
builder.Services.AddDbContext<NvTradeContext>();
// Register other repositories
builder.Services.AddScoped<IFooRepo, FooRepository>();
builder.Services.AddScoped<IBarRepo, BarRepository>();
// 2024-12-04 Prepare switch to Dapper
builder.Services.AddScoped<DatabaseRepository>();

builder.Services.AddHttpContextAccessor();

builder.Services.AddAuthentication(authOpt =>
    {
        authOpt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
        authOpt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
    })
    .AddJwtBearer(options =>
    {
        options.Authority = "https://localhost:7178/";
        options.Audience = "http://localhost:5057/";
    });

// add jwt token for later
builder.Services.AddAuthorization(options =>
{
    var policies = builder.Configuration.GetSection("Policies:Permissions").Get<List<string>>();
    policies.ForEach(p => 
    { 
        options.AddPolicy(p, policy => 
        { 
            policy.AddRequirements(new IsInRoleRequirement(p)); 
        }); 
    });
});

builder.Services.AddTransient<IAuthorizationHandler, IsInRoleAuthorizationHandler>();

var app = builder.Build();


app.UseStatusCodePages(async context =>
{
    var response = context.HttpContext.Response;
    if (response.StatusCode is (int)HttpStatusCode.Unauthorized or (int)HttpStatusCode.Forbidden)
        response.Redirect("/Info?error=Unauthorized");
});

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");     
    app.UseHsts();
}

using (var scope = app.Services.CreateScope())
{
    var services = scope.ServiceProvider;
    var context = services.GetRequiredService<NvTradeContext>();    
}


app.UseStaticFiles();
app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapRazorPages();
app.MapControllers(); 

app.Run();

Update for .net-8.0

I switched to .net-8.0 updated csproj-file and ran

dotnet publish -p:TargetFramework=net8.0 
               -p:Nullable=enable -p:FileVersion=1.0.4.0 
       --self-contained 
       --runtime win-x64

This solved the MissingMethodException Error but all styles and javascript files are 404 not found

How can i include all static files under wwwroot so that they are part of the selfcontained exe

C:devmyappwwwroot>tree
C:.
├───css
│   └── site.css
├───js
│   ├── FilterTable.js
│   └── site.js
├───lib
│   ├───bootstrap
│   │   └───dist
│   │       ├───css
│   │       └───js
│   ├───jquery
│   │   └───dist
│   ├───jquery-validation
│   │   └───dist
│   └───jquery-validation-unobtrusive
└───static

How to fix MissingMethodException

  • What causes this?
  • How to fix self contained with .net 9?
  • How to include static assets with .net 8?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật