public static Microsoft.Extensions.Configuration.IConfigurationBuilder AddYamlFile( this Microsoft.Extensions.Configuration.IConfigurationBuilder builder , Microsoft.Extensions.FileProviders.IFileProvider provider , string path , bool optional , bool reloadOnChange) { if (provider == null && System.IO.Path.IsPathRooted(path)) { provider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider( System.IO.Path.GetDirectoryName(path) ); path = System.IO.Path.GetFileName(path); } Microsoft.Extensions.Configuration.Yaml.YamlConfigurationSource source = new Microsoft.Extensions.Configuration.Yaml.YamlConfigurationSource { FileProvider = provider, Path = path, Optional = optional, ReloadOnChange = reloadOnChange }; builder.Add(source); return(builder); }
public static FileServicesOptions AddFileProviderPhysicalPathReadOnly(this FileServicesOptions app, System.Func <System.IServiceProvider, string> func) { return(AddFileProviderReadOnly(app, sp => { var env = sp.GetService <Microsoft.AspNetCore.Hosting.IHostingEnvironment>(); var fileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(func(sp)); return fileProvider; })); }
/// <summary> /// Sets skin configuration source. /// </summary> /// <param name="fullPath">Full path to the skin configuration file.</param> public void SetSource(string fullPath) { if (fullPath == null) { throw new ArgumentNullException(nameof(fullPath)); } Path = System.IO.Path.GetFileName(fullPath); FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(System.IO.Path.GetDirectoryName(fullPath)); }
// 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.UseFileServer(); //// this will serve up node_modules var provider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider( Path.Combine(_applicationPath, "node_modules") ); var _fileServerOptions = new FileServerOptions(); _fileServerOptions.RequestPath = "/node_modules"; _fileServerOptions.StaticFileOptions.FileProvider = provider; _fileServerOptions.EnableDirectoryBrowsing = true; app.UseFileServer(_fileServerOptions); app.Use(async(context, next) => { await next(); if (context.Response.StatusCode == 404 && !Path.HasExtension(context.Request.Path.Value)) { context.Request.Path = "/index.html"; await next(); } }); app.UseStaticFiles(); //app.UseIdentity(); app.UseDefaultFiles(); app.UseMvc(routes => { }); ////app.UseSwagger(c => ////{ //// c.PreSerializeFilters.Add((swagger, httpReq) => swagger.Host = httpReq.Host.Value); ////}); ////app.UseSwaggerUI(c => ////{ //// c.SwaggerEndpoint("/swagger/v1/swagger.json", "BizApi Docs"); ////}); //// Enable middleware to serve generated Swagger as a JSON endpoint. //app.UseSwagger(); //// Enable middleware to serve swagger-ui (HTML, JS, CSS etc.), specifying the Swagger JSON endpoint. //app.UseSwaggerUI(c => //{ // c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); //}); }
public IActionResult GetSwaggerSpec(string format) { if (format.Equals("json")) { var provider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(System.IO.Directory.GetCurrentDirectory()); var contents = provider.GetDirectoryContents(string.Empty); var fileInfo = provider.GetFileInfo("Swagger.json"); return(Ok(fileInfo.CreateReadStream())); } return(BadRequest()); }
public Microsoft.AspNetCore.Mvc.IActionResult DeleteConfirmed(System.Guid id) { var pie = UnitOfWork.BethanyPieShopUnitOfWork.PieRepository.GetById(id); if (pie.ImageName != null) { var filePath = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "wwwroot", "Images")) .Root + $@"\{pie.ImageName}"; System.IO.File.Delete(filePath); } UnitOfWork.BethanyPieShopUnitOfWork.PieRepository.Delete(pie); UnitOfWork.BethanyPieShopUnitOfWork.PieRepository.Save(); return(RedirectToAction(nameof(Index))); }
private void CacheFileContent() { // Looks from the project root - D:\Projects\GitHubFree\asp-net-core-samples\Tutorialsteacher\RazorTagHelpersNetCore\example.txt var filePath = @"Pages\StateManagement\example.txt"; var fileContent = System.IO.File.ReadAllText(filePath); var fileInfo = new System.IO.FileInfo(filePath); var fileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(fileInfo.DirectoryName); // Calling Dispose stops the token from listening for further changes and releases the token's resources. var changeToken = fileProvider.Watch(fileInfo.Name); var options = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(20)).AddExpirationToken(changeToken); _cache.Set("ExampleFileContent", fileContent, options); string fileContentString = _cache.Get <string>("ExampleFileContent"); }
public static void Main(string[] args) { var port = 80; if (args.Length > 0) { try { port = Convert.ToInt32(args[0]); } catch { } } LoadHtml(); try { IDisposable regiser = null; Action <object> callback = null; var fileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(root); callback = _ => { if (regiser != null) { regiser.Dispose(); } System.Threading.Tasks.Task.Delay(100).Wait(); LoadHtml(); regiser = fileProvider.Watch(index).RegisterChangeCallback(callback, null); }; regiser = fileProvider.Watch(index).RegisterChangeCallback(callback, null); } catch { Console.WriteLine("Watch file change is error!"); } var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseStartup <Startup>() .UseKestrel(o => { o.Listen(System.Net.IPAddress.Any, port <= 0 ? 80 : port); }) .Build(); host.Run(); }
public Microsoft.AspNetCore.Mvc.IActionResult Create(ViewModels.PieViewModel viewModel) { if (ModelState.IsValid) { Models.Pie newPie = new Models.Pie() { Name = viewModel.Name, ShortDescription = viewModel.ShortDescription, LongDescription = viewModel.LongDescription, AllergyInformation = viewModel.AllergyInformation, Price = viewModel.Price, CatagoryId = viewModel.CatagoryId, IsPieOfTheWeek = viewModel.IsPieOfTheWeek, InStock = viewModel.InStock, }; if (viewModel.ImageUpload != null) { newPie.ImageName = $"{newPie.Id}{System.IO.Path.GetExtension(viewModel.ImageUpload.FileName)}"; var filePath = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "wwwroot", "Images")) .Root + $@"\{newPie.ImageName}"; using (System.IO.FileStream fileStream = System.IO.File.Create(filePath)) { viewModel.ImageUpload.CopyTo(fileStream); fileStream.Flush(); } } UnitOfWork.BethanyPieShopUnitOfWork.PieRepository.Insert(newPie); UnitOfWork.BethanyPieShopUnitOfWork.PieRepository.Save(); return(RedirectToAction(actionName: "Index", controllerName: "Pies", routeValues: new { Areas = "Administration " })); } var PieCatagorySelectList = UnitOfWork.BethanyPieShopUnitOfWork.CatagoryRepository.Get(); ViewData["CatagoryId"] = new Microsoft.AspNetCore.Mvc.Rendering.SelectList(PieCatagorySelectList, Resources.DataDictionary.ID, "CatagoryName", ""); return(View(model: viewModel)); }
public async Task<IActionResult> DeleteConfirmed(Guid id) { var user = UnitOfWork.BethanyPieShopUnitOfWork.UserRepository.GetById(id); if (user.ImageName != null) { var filePath = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "wwwroot", "Images")) .Root + $@"\{user.ImageName}"; System.IO.File.Delete(filePath); } if (User.Identity.Name.ToLower() == user.Username.ToLower()) { var currentUser = await userManager.FindByNameAsync(User.Identity.Name); await userManager.DeleteAsync(currentUser); await signInManager.SignOutAsync(); } else { var aspUser= await userManager.FindByNameAsync(user.Username); await userManager.DeleteAsync(aspUser); } UnitOfWork.BethanyPieShopUnitOfWork.UserRepository.Delete(user); UnitOfWork.BethanyPieShopUnitOfWork.UserRepository.Save(); return RedirectToAction(nameof(ListAllUsers)); }
public async Task <Microsoft.AspNetCore.Mvc.IActionResult> Registrar(ViewModels.RegisterViewModel viewModel) { var roleSelectList = await roleManager.Roles.ToListAsync(); System.Collections.Generic.List <string> roleNames = new System.Collections.Generic.List <string>(); if (ModelState.IsValid) { var user = new IdentityUser() { UserName = viewModel.Username, NormalizedUserName = viewModel.Username.ToUpper(), Email = viewModel.EMail, EmailConfirmed = true }; var newUser = new Models.User() { Username = viewModel.Username, Password = viewModel.Password, EMail = viewModel.EMail, PhoneNumber = viewModel.PhoneNumber, FullName = viewModel.FullName, Address = viewModel.Address, City = viewModel.City, Country = viewModel.Country, ZipCode = viewModel.ZipCode, State = viewModel.State, IsActive = viewModel.IsActive, //RoleId =Guid.Parse( roleSelectList.Where(current => current.Name == Resources.DataDictionary.RoleName3).FirstOrDefault().Id), TotalShoping = 0, }; if (viewModel.ImageUpload != null) { newUser.ImageName = $"{newUser.Id}{System.IO.Path.GetExtension(viewModel.ImageUpload.FileName)}"; var filePath = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "wwwroot", "Images")) .Root + $@"\{newUser.ImageName}"; using (System.IO.FileStream fileStream = System.IO.File.Create(filePath)) { viewModel.ImageUpload.CopyTo(fileStream); fileStream.Flush(); } } UnitOfWork.BethanyPieShopUnitOfWork.UserRepository.Insert(newUser); var isSavedSuccessfully = UnitOfWork.BethanyPieShopUnitOfWork.UserRepository.Save(); var result = await userManager.CreateAsync(user, viewModel.Password); if (isSavedSuccessfully && result.Succeeded) { return(RedirectToAction(actionName: "Index", controllerName: "Home")); } foreach (var error in result.Errors) { ModelState.AddModelError("", error.Description); } } //var roleSelectList = await roleManager.Roles.ToListAsync(); //ViewData["RoleId"] = new Microsoft.AspNetCore.Mvc.Rendering.SelectList(roleSelectList, "Id", "Name",viewModel.RoleId); return(View(viewModel)); }
public Microsoft.AspNetCore.Mvc.IActionResult EditProfile(System.Guid id, ViewModels.ProfileViewModel viewModel) { if (id != viewModel.Id) { return(NotFound()); } var orginalUser = UnitOfWork.BethanyPieShopUnitOfWork.UserRepository.GetById(viewModel.Id); if (ModelState.IsValid) { try { orginalUser.Id = viewModel.Id; orginalUser.Username = viewModel.Username; orginalUser.FullName = viewModel.FullName; orginalUser.EMail = viewModel.EMail; orginalUser.Address = viewModel.Address; orginalUser.City = viewModel.City; orginalUser.Country = viewModel.Country; orginalUser.PhoneNumber = viewModel.PhoneNumber; orginalUser.ZipCode = viewModel.ZipCode; //orginalUser.RoleId = viewModel.RoleId; orginalUser.State = viewModel.State; orginalUser.IsActive = viewModel.IsActive; if (viewModel.ImageUpload != null) { if (orginalUser.ImageName != null) { var oldfilePath = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "wwwroot", "Images")) .Root + $@"\{orginalUser.ImageName}"; System.IO.File.Delete(oldfilePath); } orginalUser.ImageName = $"{orginalUser.Id}{System.IO.Path.GetExtension(viewModel.ImageUpload.FileName)}"; var newfilePath = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "wwwroot", "Images")) .Root + $@"\{orginalUser.ImageName}"; using (System.IO.FileStream fileStream = System.IO.File.Create(newfilePath)) { viewModel.ImageUpload.CopyTo(fileStream); fileStream.Flush(); } } UnitOfWork.BethanyPieShopUnitOfWork.UserRepository.Update(orginalUser); UnitOfWork.BethanyPieShopUnitOfWork.UserRepository.Save(); } catch (DbUpdateConcurrencyException) { if (UnitOfWork.BethanyPieShopUnitOfWork.UserRepository.IsExist(id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(ShowProfileInformation), "Account")); } //var roles =await roleManager.Roles.ToListAsync(); //ViewData["RoleId"] = new Microsoft.AspNetCore.Mvc.Rendering.SelectList(roles, "Id", "Name", viewModel.RoleId); return(View(viewModel)); }
public Microsoft.AspNetCore.Mvc.IActionResult Edit(System.Guid id, ViewModels.PieViewModel viewModel) { if (id != viewModel.Id) { return(NotFound()); } if (ModelState.IsValid) { try { var orginalPie = UnitOfWork.BethanyPieShopUnitOfWork.PieRepository.GetById(viewModel.Id); orginalPie.Id = viewModel.Id; orginalPie.Name = viewModel.Name; orginalPie.ShortDescription = viewModel.ShortDescription; orginalPie.LongDescription = viewModel.LongDescription; orginalPie.AllergyInformation = viewModel.AllergyInformation; orginalPie.CatagoryId = viewModel.CatagoryId; orginalPie.Price = viewModel.Price; orginalPie.IsPieOfTheWeek = viewModel.IsPieOfTheWeek; orginalPie.InStock = viewModel.InStock; //orginalPie.ImageName = viewModel.ImageName; oops!BeCarfull we should not initilize it here if (viewModel.ImageUpload != null) { if (viewModel.ImageName != null) { var oldfilePath = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "wwwroot", "Images")) .Root + $@"\{orginalPie.ImageName}"; System.IO.File.Delete(oldfilePath); } orginalPie.ImageName = $"{orginalPie.Id}{System.IO.Path.GetExtension(viewModel.ImageUpload.FileName)}"; var newfilePath = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "wwwroot", "Images")) .Root + $@"\{orginalPie.ImageName}"; using (System.IO.FileStream fileStream = System.IO.File.Create(newfilePath)) { viewModel.ImageUpload.CopyTo(fileStream); fileStream.Flush(); } } UnitOfWork.BethanyPieShopUnitOfWork.PieRepository.Update(orginalPie); UnitOfWork.BethanyPieShopUnitOfWork.PieRepository.Save(); } catch (Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException) { if (UnitOfWork.BethanyPieShopUnitOfWork.PieRepository.IsExist(id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(actionName: "Index", controllerName: "Pies", routeValues: new { Areas = "Administration " })); } var PieCatagorySelectList = UnitOfWork.BethanyPieShopUnitOfWork.CatagoryRepository.Get(); ViewData["CatagoryId"] = new Microsoft.AspNetCore.Mvc.Rendering.SelectList(PieCatagorySelectList, Resources.DataDictionary.ID, "CatagoryName", viewModel.CatagoryId); return(View(viewModel)); }