DEV Community

Cover image for ASP .NET Core Project details
Karen Payne
Karen Payne

Posted on

ASP .NET Core Project details

Introduction

Learn how to read attributes from a project file using a class project that works with any .NET Core project without passing the project location or project name.

Sample project file

Usage idea

  • Dynamic copyright in _Layout page
  • Information in an about page

In the page below "Project Details" and footer are read from the project file.

Page rendering information from the project file

In _Layout.cshtml Footer

<footer class="border-top footer text-muted">
    <div class="container">
        @ProjectInformation.GetCopyright() | @ProjectInformation.GetCompany() - <a asp-area="" asp-page="/Privacy">Privacy</a>
    </div>
</footer>
Enter fullscreen mode Exit fullscreen mode

In the about page (ProjectDetails) backend, read information.

using Microsoft.AspNetCore.Mvc.RazorPages;
using CommonLibrary.Models;
using static CommonLibrary.ProjectInformation;

namespace ProjectPropertiesSampleApp.Pages;

public class ProjectDetailsModel : PageModel
{
    /// <summary>
    /// Sets the details of the project, including its name, company, copyright information, and version.
    /// </summary>
    /// <value>
    /// A <see cref="CommonLibrary.Models.ProjectDetails"/> object containing the project's metadata.
    /// </value>
    public required ProjectDetails Details { get; set; } = new()
    {
        Copyright = GetCopyright(),
        ProjectName = GetProduct(),
        Company = GetCompany(),
        Version = GetVersion(),
        Title = GetTitle(),
        ProjectDescription = GetProjectDescription()
    };

    public void OnGet()
    {
    }
}
Enter fullscreen mode Exit fullscreen mode

Front-end code.

How to use

Source code

The easy way is to copy CommonLibrary to a Microsoft Visual Studio solution, followed by referencing CommonLibrary, while a better way is to create a local NuGet package on a network drive or on a development machine, where the choice depends on whether this is for a single developer or a team of developers.

Note
See the following for setting up local packages

The absolute best way to implement from here is to run through the provided sample front-end project code. Take note of the using statements, for instance in _layout.cshtml the following @using CommonLibrary.

Adding attributes

In this example, add the following under Title attribute in the project file.

<SomeDetail>Enter information here</SomeDetail>
Enter fullscreen mode Exit fullscreen mode

In CommonLibrary.Models.ProjectDetails class add the SomeDetail property.

public class ProjectDetails
{
    public string Copyright { get; set; }
    public string ProjectName { get; set; }
    public string ProjectDescription { get; set; }
    public string Company { get; set; }
    public Version Version { get; set; }
    public string Title { get; set; }
    public string SomeDetail { get; set; } // add this property
}
Enter fullscreen mode Exit fullscreen mode

Next, in CommonLibrary.ProjectInformationBackend.cs, make a copy of ReadDescriptionFromProjectFile and name it ReadSomeDetailFromProjectFile followed by modifying as shown below.

private static string ReadSomeDetailFromProjectFile(string projectFilePath)
{
    if (!File.Exists(projectFilePath))
        return "No SomeDetail information found.";

    var doc = XDocument.Load(projectFilePath);

    // SDK-style projects (no MSBuild namespace)
    var description = doc
        .Descendants("PropertyGroup")
        .Elements("SomeDetail")
        .Select(e => e.Value)
        .FirstOrDefault();

    if (!string.IsNullOrWhiteSpace(description))
        return description;

    // Old-style .csproj with MSBuild namespace
    XNamespace msbuild = "http://schemas.microsoft.com/developer/msbuild/2003";

    description = doc
        .Descendants(msbuild + "PropertyGroup")
        .Elements(msbuild + "SomeDetail")
        .Select(e => e.Value)
        .FirstOrDefault();

    return string.IsNullOrWhiteSpace(description)
        ? "No SomeDetail information found."
        : description;
}
Enter fullscreen mode Exit fullscreen mode

In CommonLibrary.ProjectInformation add

[MethodImpl(MethodImplOptions.NoInlining)]
public static string GetSomeDetail()
{
    var asm = Assembly.GetCallingAssembly();
    var projectFile = TryFindProjectFile(asm);

    if (projectFile is null)
        return "No SomeDetail information found.";

    return ReadSomeDetailFromProjectFile(projectFile);
}
Enter fullscreen mode Exit fullscreen mode

Test it in Index.cshtml.cs OnGet Console.WriteLine(ProjectInformation.GetSomeDetail());

Summary

The provided instructions and code sample are easy to follow, allowing developers to present data from a project file with minimal effort.

Top comments (0)