Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net core mvc: split hosting and business-logic/ui into separate projects

I want to separate my solution into at least two parts:

  • Hosting-technology (Initializing Kestrel and setting up all the middleware, e.g. swashbuckle, authentication)
  • Business-Logic & UI

because I want the hosting configuration to be replacable in later stages of the development process.

I tried simply moving all the folders containing controllers, models and views into a separate project, like shown in the image below:

Two projects with hosting configuration and business-logic separated:

enter image description here

So I

  • moved those folders to the *.Implementation project
  • added a nuget-reference to the package "Microsoft.AspNetCore.Mvc"
  • referenced the *.Implementation project from the *.Host project
  • added this class to the "Controllers"-folder in the *.Implementation project:
using Microsoft.AspNetCore.Mvc;

namespace MyApp.Implementation.Controllers
{
    public class ExampleController : Controller
    {
        public ActionResult<int> Index()
        {
            return 5;
        }
    }
}

If I start the application and open http://localhost:5000/example in my browser, I get the result "5" in my browser. This shows to me that the hosting technology finds my controller in the separate project.

But when I open http://localhost:5000 in the browser, I get an exception page telling me that the views for the Home-Controller where not found. The Console also shows the exception:

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
      An unhandled exception has occurred while executing the request.
System.InvalidOperationException: The view 'Index' was not found. The following locations were searched:
/Views/Home/Index.cshtml
/Views/Shared/Index.cshtml
/Pages/Shared/Index.cshtml

Since the webhost finds my controller, I would expect that it finds the views too. It seems not so.

How can I tell the webhost where to look for the views instead? Or do I need to do anything to them instead?

like image 838
Ravior Avatar asked Apr 30 '19 12:04

Ravior


People also ask

Should you split your ASP NET MVC project into multiple projects?

It shouldn't! That's why it's designed in a modular way. In most web applications out there, we version and deploy all these assemblies (Web, BLL and DAL) together. So, separating a project into 3 projects does not add any values.

Does ASP Net has the ability to separate layout and business logic?

ASP.NET can provide separation between data access, business logic, and presentation in several ways. For example, the data source model, which includes server controls such as the LinqDataSource and ObjectDataSource controls, separates the presentation layer from the data-access code and the business logic.

Where do you put business logic in MVC?

The business logic should be placed in the model, and we should be aiming for fat models and skinny controllers. As a start point, we should start from the controller logic. For example: on update, your controller should direct your code to the method/service that delivers your changes to the model.

Where do you usually put your business logic?

Business logic should live in the data model. And, what's more, it should live in the graph data model because that's the right abstraction for the next twenty years.


1 Answers

In addition to Kirk Larkin's comment to look at Application Parts in ASP.NET Core, you might also want to check out Razor Class Libraries.

I have not tried it myself yet, but it looks it might provide a solution for your issue.

like image 75
Bart Hofland Avatar answered Sep 19 '22 03:09

Bart Hofland