So i'm trying to upload my images via a API to the folder and for that i need my environment which wouldn't work.
The error that i get:
Unable to resolve service for type 'Microsoft.AspNet.Hosting.IHostingEnvironment' while attempting to activate 'cms.Controllers.PageController'.
My code (it goes wrong when i put the iHostingEnviroment in the controller)
namespace cms.Controllers { [Route("/api/[controller]")] public class PageController : Controller { ets1Context _context; UserManager<ApplicationUser> _userManager; private readonly IHostingEnvironment _env; public object PageResourceJson { get; private set; } public PageController(ets1Context context, UserManager<ApplicationUser> userManager, IHostingEnvironment env) { _context = context; _userManager = userManager; _env = env; } [Route("/api/image/[controller]")] [HttpPost] public IActionResult UploadFilesAjax() { long size = 0; var files = Request.Form.Files; foreach (var file in files) { var filename = ContentDispositionHeaderValue .Parse(file.ContentDisposition) .FileName .Trim('"'); filename = _env.WebRootPath + $@"\{filename}"; size += file.Length; using (FileStream fs = System.IO.File.Create(filename)) { file.CopyTo(fs); fs.Flush(); } } return Ok(); } } }
My startup ( ther las time i got an ae:
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using cms.Data; using cms.Models; using cms.Services; using cms.Entity; namespace cms { public class Startup { public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); if (env.IsDevelopment()) { // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709 builder.AddUserSecrets(); // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately. builder.AddApplicationInsightsSettings(developerMode: true); } builder.AddEnvironmentVariables(); Configuration = builder.Build(); } public IConfigurationRoot Configuration { get; } //public void OnConfiguring(IServiceCollection services) //{ // services.AddMvc(); // var cs = Configuration.GetConnectionString("DatabaseCon"); // services.AddDbContext<ets1Context>(options => options.UseSqlServer(cs)); //} // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddApplicationInsightsTelemetry(Configuration); var cs = Configuration.GetConnectionString("DatabaseCon"); services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(cs)); services.AddIdentity<ApplicationUser, IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); services.AddDbContext<ets1Context>(options => options.UseSqlServer(cs)); services.AddMvc(); // Add application services. services.AddTransient<IEmailSender, AuthMessageSender>(); services.AddTransient<ISmsSender, AuthMessageSender>(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); app.UseApplicationInsightsRequestTelemetry(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); app.UseBrowserLink(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseApplicationInsightsExceptionTelemetry(); app.UseStaticFiles(); app.UseIdentity(); // Add external authentication middleware below. To configure them please see http://go.microsoft.com/fwlink/?LinkID=532715 app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Account}/{action=Login}/{id?}"); }); } } }
And my project.json:
{"buildOptions": {"emitEntryPoint": true,"preserveCompilationContext": true },"dependencies": {"Microsoft.ApplicationInsights.AspNetCore": "1.0.0","Microsoft.AspNet.Http.Extensions": "1.0.0-rc1-final","Microsoft.AspNet.Mvc.Core": "6.0.0-rc1-final","Microsoft.AspNetCore.Authentication.Cookies": "1.0.0","Microsoft.AspNetCore.Diagnostics": "1.0.0","Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0","Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0","Microsoft.AspNetCore.Mvc": "1.0.1","Microsoft.AspNetCore.Mvc.Core": "1.0.1","Microsoft.AspNetCore.Razor.Tools": {"version": "1.0.0-preview2-final","type": "build" },"Microsoft.AspNetCore.Routing": "1.0.1","Microsoft.AspNetCore.Server.IISIntegration": "1.0.0","Microsoft.AspNetCore.Server.Kestrel": "1.0.1","Microsoft.AspNetCore.StaticFiles": "1.0.0","Microsoft.EntityFrameworkCore": "1.1.0","Microsoft.EntityFrameworkCore.Design": "1.1.0","Microsoft.EntityFrameworkCore.Sqlite": "1.0.0","Microsoft.EntityFrameworkCore.Sqlite.Design": "1.0.0","Microsoft.EntityFrameworkCore.SqlServer.Design": "1.1.0","Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final","Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0","Microsoft.Extensions.Configuration.Json": "1.0.0","Microsoft.Extensions.Configuration.UserSecrets": "1.0.0","Microsoft.Extensions.DependencyInjection.Abstractions": "1.1.0","Microsoft.Extensions.Logging": "1.1.0","Microsoft.Extensions.Logging.Console": "1.0.0","Microsoft.Extensions.Logging.Debug": "1.0.0","Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0","Microsoft.Framework.DependencyInjection.Abstractions": "1.0.0-beta7","Microsoft.NETCore.App": {"version": "1.0.1","type": "platform" },"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0","Microsoft.VisualStudio.Web.CodeGeneration.Tools": {"version": "1.0.0-preview2-final","type": "build" },"Microsoft.VisualStudio.Web.CodeGenerators.Mvc": {"version": "1.0.0-preview2-final","type": "build" } },"frameworks": {"netcoreapp1.0": {"imports": ["dotnet5.6","portable-net45+win8" ] } },"publishOptions": {"include": ["wwwroot","**/*.cshtml","appsettings.json","web.config" ] },"runtimeOptions": {"configProperties": {"System.GC.Server": true } },"scripts": {"prepublish": [ "bower install", "dotnet bundle" ],"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ] },"tools": {"BundlerMinifier.Core": "2.0.238","Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final","Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final","Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final","Microsoft.EntityFrameworkCore.Tools.DotNet": " 1.1.0-preview4-final","Microsoft.Extensions.SecretManager.Tools": "1.0.0-preview2-final","Microsoft.VisualStudio.Web.CodeGeneration.Tools": {"version": "1.0.0-preview2-final","imports": ["portable-net45+win8" ] } },"userSecretsId": "aspnet-cms-dd6fca74-1002-4839-a8f7-240432a3595b" }
Is there someone who knows how to solve this??
Thanks in advance!