Click here to Skip to main content
16,022,352 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to create a project in C# MVC pattern without using the scaffolding techniques in the code and i want to create withe the manual code

What I have tried:

I have tried using the scaffolding techniques but i want to create using the manual creation
Posted

1 solution

There's nothing magic in working with C# - there are no binary files hidden behind the scenes when you start creating your C# applications. The answer to what you are asking is; it depends. If you just want to try out some casual C# statements and aren't bothered about this becoming an application, you could try LINQPad[^]. This utility is, basically, a C# scratchpad conveniently bundled together so you could write code like this without any trouble
C#
List<int> values = new();
values.Add(10);
values.Add(5);
values.Add(7);
foreach (int value in values)
{
  Console.WriteLine(value);
}
If, however, you want to write something that persists - and, from your question we can see that you do, that's no trouble. As long as you are using a relatively up to date version of dotnet. Open up a text editor of your choice, and start writing the various files. You could start with your csproj file. For this example, I'm going to create a C# project file as if I were building a .NET 8 web api.
XML
<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.4" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
  </ItemGroup>

</Project>
Next, create an appsettings.json file like so:
JSON
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}
Create a Properties folder and, inside that, create a filed called launchSettings.json - this is going to be used when you actually run your project
JSON
{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:63483",
      "sslPort": 44330
    }
  },
  "profiles": {
    "http": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "http://localhost:5187",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "https": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "https://localhost:7126;http://localhost:5187",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}
Finally, add your Program.cs file:
C#
WebApplicationBuilder? builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

WebApplication app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

string[] summaries = new[]
{
    "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

app.MapGet("/weatherforecast", () =>
{
    var forecast =  Enumerable.Range(1, 5).Select(index =>
        new WeatherForecast
        (
            DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
            Random.Shared.Next(-20, 55),
            summaries[Random.Shared.Next(summaries.Length)]
        ))
        .ToArray();
    return forecast;
})
.WithName("GetWeatherForecast")
.WithOpenApi();

app.Run();
Finally, run the following command:
dotnet build
Congratulations, you have just built your own API style application entirely from scratch. With only a little bit of work, you could adapt the technique I followed here to build your MVC program complete with MVC support.
 
Share this answer
 
Comments
[no name] 7-Jun-24 2:55am    
I see the beginnings of a very useful article there. Far too many devs use the auto-generated frameworks and have no idea what is going on underneath (and that is not just restricted to .NET).
Pete O'Hanlon 7-Jun-24 3:23am    
I must admit, I didn't even think of that. As always, you're correct, this could be a useful article.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900