destro
Well-known member
- Joined
- Mar 28, 2020
- Messages
- 46
- Programming Experience
- 1-3
I am trying to upload image to wwwroot path using my homecontroller but my IhostingEnvironment interface has no such property named WebRootPath, infact it has no properties at all when I peeked the definition. How do I access this path in my HomeController?
Startup.cs:
public class Startup
{
private IConfiguration _config;
public Startup(IConfiguration config)
{
_config = config;
}
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IAstrologerRepository, SQLAstrologerRepository>();
services.AddMvc();
services.AddDbContextPool<ApplicationDbContext>(options =>
options.UseSqlServer(_config.GetConnectionString("DefaultConnection")));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAuthentication();
app.UseCookiePolicy();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(name: "login",
template: "{controller=Home}/{action=Login}");
});
}
}
HomeController.cs:
[CODE lang="csharp" title="Startup.cs"]
public class HomeController : Controller
{
private readonly IAstrologerRepository _astrologerRepository;
private readonly IHostingEnviroment _hostingEnviroment;
public HomeController(IAstrologerRepository astrologerRepository, IHostingEnviroment hostingEnviroment)
{
_astrologerRepository = astrologerRepository;
_hostingEnviroment = hostingEnviroment;
}
// GET: /<controller>/
public ViewResult Index()
{
var model = _astrologerRepository.GetAllAstrologers();
return View(model);
}
[HttpGet]
public ViewResult SignUp()
{
return View();
}
[HttpPost]
public IActionResult SignUp(AstrologerCreateViewModel model)
{
if (ModelState.IsValid)
{
// string fileName = null;
if(model.profilephoto!=null)
{
}
// Astrologer newastrologer = _astrologerRepository.Add(astrologer);
return RedirectToAction("Index");
}
return View();
}
}