I am new to C# and asp.net core and I am trying to create a simple repository pattern and pass a string
value but I'm getting the error
An unhandled exception occurred while processing the request. InvalidOperationException: Unable to resolve service for type 'HomepageRepository' while attempting to activate 'WebApplication.Controllers.HomeController'. GetService
This is my code
public static class DBConnection
{
public static string connectDB(this string connectionString)
{
return connectionString = "pass value of this string";
}
}
HomePageRepository
: I would like to pass the value of connectionString
here
public class HomepageRepository : IHomepageRepository
{
private string connectionString;
public HomepageRepository()
{
connectionString= connectionString.connectDB();
}
public string Streams()
{
return connectionString;
}
}
Then pass this to my controller like this
namespace WebApplication.Controllers
{
public class HomeController : Controller
{
public HomepageRepository _repository;
public HomeController(HomepageRepository repository)
{
_repository = repository;
}
public string Streams()
{
_repository.Streams();
return "hello";
}
}
}
Again I am very new to C# and this code above gives only a runtime error . All I am trying to do is pass the connectionString
value in DBConnection
to HomePageRepository
then pass it into a controller in HomeController
. Any suggestions would be great.