Skip to content

YAF.NET Integration in to an existing ASP.NET Core Application

Ingo edited this page Mar 2, 2024 · 11 revisions

If you want to integrate YAF.NET in to an existing .NET (Core) 8 Application there is an Sample Application available

https://github.com/YAFNET/YAF.SampleWebApplication/releases

This Application is a Standard Visual Studio Sample ASP.NET Core Application, including YAF.NET integrated Area

If you want to manually integrate YAF.NET here is an very quick Guide on how to do it...

Quick Guide

Add the YAF Nuget Packages

  1. Install the YAFNET.RazorPages Package via Nuget Browser or the console

NuGet

> dotnet add package YAFNET.RazorPages
  1. Depending on which Data Provider you want top use add the package ..

YAFNET.Data.SqlServer Package via Nuget Browser or the console

NuGet

> dotnet add package YAFNET.Data.SqlServer

YAF.Data.MySQL Package via Nuget Browser or the console

NuGet

> dotnet add package YAFNET.Data.MySQL

YAFNET.Data.PostgreSQL Package via Nuget Browser or the console

NuGet

> dotnet add package YAFNET.Data.PostgreSQL

YAFNET.Data.Sqlite Package via Nuget Browser or the console

NuGet

> dotnet add package YAFNET.Data.Sqlite

Getting Started

Program.cs

In the Program.cs add UseAutofacServiceProviderFactory and ConfigureYafLogging to the IHostBuilder.

 var host = Host.CreateDefaultBuilder(args).UseAutofacServiceProviderFactory()
     .ConfigureYafLogging()
     .ConfigureWebHostDefaults(webHostBuilder => webHostBuilder.UseStartup<Startup>()).Build();

 return host.RunAsync();

Startup.cs

You add the YAF.NET services and the YAF.NET UI in the Startup.cs of your application.

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddRazorPages(options =>
    {
      options.Conventions.AddPageRoute("/SiteMap", "Sitemap.xml");
    }).AddYafRazorPages(this.Environment);

    services.AddControllers();

    services.AddSignalR();

    services.AddYafCore(this.Configuration);
    ...
}

/// <summary>
/// Configures the container.
/// </summary>
/// <param name="builder">The builder.</param>
public void ConfigureContainer(ContainerBuilder builder)
{
    builder.RegisterYafModules();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{

    app.RegisterAutofac();

    app.UseAntiXssMiddleware();

    app.UseStaticFiles();

    app.UseSession();

    app.UseYafCore(this.ServiceLocator, env);

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapRazorPages();
        endpoints.MapAreaControllerRoute(
            name: "default", 
            areaName:"Forums",
            pattern: "{controller=Home}/{action=Index}/{id?}");
        endpoints.MapControllers();

        endpoints.MapYafHubs();
    });
    ...
}

Now the Forum is ready and you can add a link to your _Layout.cshtml

 <li class="nav-item">
     <a class="nav-link" asp-area="Forums" asp-page="/Index">Forums</a>
 </li>