I want to separate my solution into at least two parts:
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:
So I
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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With