Skip to content

First Application

Huy Nguyen edited this page Apr 5, 2025 · 2 revisions

Creating Your First Application

This tutorial will guide you through creating your first Mixcore CMS application from scratch.

Prerequisites

Step 1: Create a New Application

  1. Open your terminal and navigate to your workspace:

    mkdir MyFirstMixcoreApp
    cd MyFirstMixcoreApp
  2. Create a new Mixcore application:

    dotnet new mixcore-app -n MyFirstApp
  3. Add the application to the solution:

    dotnet sln add MyFirstApp/MyFirstApp.csproj

Step 2: Configure the Application

  1. Open appsettings.json in your application:

    {
      "ConnectionStrings": {
        "DefaultConnection": "Server=localhost;Database=mixcore;Trusted_Connection=True;"
      },
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      },
      "AllowedHosts": "*"
    }
  2. Update the database connection string to match your environment.

Step 3: Create Your First Module

  1. Create a new module using the Mixcore CLI:

    dotnet new mixcore-module -n MyFirstModule
  2. Add the module to your application:

    dotnet add MyFirstApp/MyFirstApp.csproj reference MyFirstModule/MyFirstModule.csproj
  3. Register the module in Startup.cs:

    public void ConfigureServices(IServiceCollection services)
    {
        // ... existing code ...
        services.AddMixcoreModule<MyFirstModule>();
    }

Step 4: Create a Simple Page

  1. Create a new page in your module:

    // MyFirstModule/Pages/Index.cshtml.cs
    public class IndexModel : PageModel
    {
        public void OnGet()
        {
            ViewData["Title"] = "My First Mixcore Page";
        }
    }
  2. Create the corresponding view:

    @* MyFirstModule/Pages/Index.cshtml *@
    @page
    @model MyFirstModule.Pages.IndexModel
    @{
        ViewData["Title"] = "My First Mixcore Page";
    }
    
    <div class="container">
        <h1>Welcome to My First Mixcore Application!</h1>
        <p>This is your first custom page in Mixcore CMS.</p>
    </div>

Step 5: Add Custom Functionality

  1. Create a service interface:

    // MyFirstModule/Services/IMyService.cs
    public interface IMyService
    {
        string GetWelcomeMessage();
    }
  2. Implement the service:

    // MyFirstModule/Services/MyService.cs
    public class MyService : IMyService
    {
        public string GetWelcomeMessage()
        {
            return "Hello from MyService!";
        }
    }
  3. Register the service in your module:

    // MyFirstModule/Module.cs
    public class Module : IMixcoreModule
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddScoped<IMyService, MyService>();
        }
    }
  4. Use the service in your page:

    // MyFirstModule/Pages/Index.cshtml.cs
    public class IndexModel : PageModel
    {
        private readonly IMyService _myService;
    
        public IndexModel(IMyService myService)
        {
            _myService = myService;
        }
    
        public string WelcomeMessage { get; set; }
    
        public void OnGet()
        {
            WelcomeMessage = _myService.GetWelcomeMessage();
            ViewData["Title"] = "My First Mixcore Page";
        }
    }
  5. Update the view to display the message:

    @* MyFirstModule/Pages/Index.cshtml *@
    <div class="container">
        <h1>Welcome to My First Mixcore Application!</h1>
        <p>@Model.WelcomeMessage</p>
    </div>

Step 6: Run and Test

  1. Build the application:

    dotnet build
  2. Run the application:

    dotnet run --project MyFirstApp
  3. Open your browser and navigate to:

    http://localhost:5000
    

Next Steps

Need Help?

Clone this wiki locally