'Calling Another Project's Controller From A Project In The Same Solution (.NET Core )

There are 2 projects in the same solution. First project is a .NET Core project and it has all the codes(controllers, models etc.) related to packages. I need to get the information (id, name, description) of the packages and display it in the second project(.NET Core Web App with Razor). Is it possible to do it without changing the first project? I only want to show the package list on a single web page. I tried calling the first project's controller but it didn't work. Maybe I missed a point. Any help is appreciated.



Solution 1:[1]

This requirement can be achieved, please see the gif image below.

Tips

If you want to call another project's controller from a project in the same solution, you need to make sure there is in HomeController in both project. I mean the name of any class should be unique in both projects.

Otherwise you will face the same issue like my homepage.

enter image description here

Test Code:

public List<PackageReference> GetPackageList5(string projectname) 
{
    List<PackageReference> list = new List<PackageReference>();
    PackageReference p = null;
    XDocument doc = XDocument.Load(_webHostEnvironment.ContentRootPath+ "/"+ projectname + ".csproj");

    var packageReferences = doc.XPathSelectElements("//PackageReference")
        .Select(pr => new PackageReference
        {
            Include = pr.Attribute("Include").Value,
            Version = pr.Attribute("Version").Value
        });

    Console.WriteLine($"Project file contains {packageReferences.Count()} package references:");
    foreach (var packageReference in packageReferences)
    {
        p = new PackageReference();
        p.Version= packageReference.Version;
        p.Include= packageReference.Include;
        list.Add(packageReference);
        //Console.WriteLine($"{packageReference.Include}, version {packageReference.Version}");
    }
    return list;
}

My Test Steps:

  1. create two project, Net5MVC,Net6MVC

    enter image description here

  2. add project reference.

    enter image description here

    enter image description here

    enter image description here

  3. My .net6 project references a .net5 project. So in my HomeController (.net), I add below:

    enter image description here

    using Net5MVC.ForCore6;
    using Net5MVC.Models;
    

Suggestion

  1. When we reference the .net5 project in .net6 project, we can build success, but when we deploy it, it always failed. The reason is some file was multiple publish output files with the same relative path.

    Found multiple publish output files with the same relative path: 
    D:\..\Net6\Net6\Net5MVC\appsettings.Development.json, 
    D:\..\Net6\Net6\Net6MVC\appsettings.Development.json, 
    D:\..\Net6\Net6\Net5MVC\appsettings.json, 
    D:\..\Net6\Net6\Net6MVC\appsettings.json.
    
  2. And usually will add class library to current project, not add a web project.

  3. As we know we can find packages info in .csproj file, so we need copy and paste .csproj file to publish folder.

  4. I still recommend using the GetPackageList5 method above as an interface for your project, using HttpClient for requests.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1