Click here to Skip to main content
15,888,048 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to have 2 projects in my solution.
One is for WebAPI and another for front-end WEB aka. Angular. I do not won't to have Controller logic in Web project, just one to call the WebAPI project URLS. Can this be done? How do I need to configure WEB project, so when I start the WEB, the WEBAPI URL-s will work.

What I have tried:

Read about referencing the WEPAPI as HTTPClient, but don't know if this is the right path, because I still have the Controller classes in my WEB projects.
Posted
Updated 5-Dec-17 3:15am

To have your api controller in a seperate dll you'll have to trigger the loading of that very early in the sequence, like in application start

GlobalConfiguration.Configuration.Services.Replace(typeof(IAssembliesResolver), new AuxAssemblyResolver());


So essentially you'll derive from a standard IAssembliesResolver and add your stuff to custom load wherever you've placed your definition.

public class AuxAssemblyResolver : IAssembliesResolver
    {
        public ICollection<Assembly> GetAssemblies()
        {
            List<Assembly> baseAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
            //Type controllerType = typeof(Business.Payments.Stripe.SubscriptionController);            
            //var controllersAssembly =  Assembly.LoadFrom(controllerType.Assembly.Location);
            //baseAssemblies.Add(controllersAssembly);
            return baseAssemblies;
        }
    }

I did something like that myself when making a subscription controller that i wanted out of the MVC project recently, the funny bit is that the assembly to load in my case a business assembly handling payments actually get's double loaded if you include the code to actively load it ... go figure, but this way loading whatever domains the the appdomain manages to pick up the externally defined assembly, presumably because the project referenced it.

I'm still a bit puzzled why making an auxilliary assemblies resolve that in effect doesn't do anything you wouldn't presume any assembly resolver would already do, does the trick but at least in my case it did.

In principle of cause actually loading the assembly is what should make the class available for instantiation so i left the do-good code commented in myself, but uncommenting it produces ambiguity and as that bit then worked i eventually decided it was not my windmill to conquer, that why.

Hope this helps
 
Share this answer
 
v2
Comments
Uffman 5-Dec-17 2:10am    
Saw this one, tnx. But this project has Controller is the same project than also has Angular Code. What I would like is to have API controllers in separate project.
Example:

SampleApp.Core
SampleApp.Infrastucture
SampleApp.Services
SampleApp.WebApi -> this project has all the logic for WeAPI Controllers
SampleApp.Angular -> this project only has Angular code

How can I call/use SampleApp.WebApi Controller in SampleApp.Angular project
Thomas Nielsen - getCore 5-Dec-17 3:12am    
I updated the solution for general external controllers, in this case a web api 2 doesn't have a difference to .net core that i have found yet, beyond more stuff working off the bat. From your decription it is the same problem i encountered.
Thomas, thanks. Found a different solution.
I needed to configure AssemblyLoad in Startup.cs

C#
public void ConfigureServices(IServiceCollection services)
{
    var controllerAssembly = Assembly.Load(new AssemblyName("DriveTogether.WebApi"));
    services.AddMvc().AddApplicationPart(controllerAssembly).AddControllersAsServices();
}

Then remove routing from Configure method and add:
C#
app.UseMvcWithDefaultRoute();

then you can call API Controller with these
http://localhost/{ControllerName}/{Method}
http://localhost/Values/Get" --> note that there is no "api" in URL
 
Share this answer
 
Comments
Thomas Nielsen - getCore 6-Dec-17 10:31am    
Yea that's right! startup.cs is the most common name for the 'application_start' class in .net core
Mobin.seven 16-Dec-19 2:22am    
In a Blazor project this looks like this:
var assembly = typeof(Department).Assembly;
services.AddControllers().AddControllersAsServices()                .PartManager.ApplicationParts.Add(new AssemblyPart(assembly));

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