Asp.net 如何在用户登录时为.net Core 2创建目录
我有一个要求,至少现在为.net核心网站创建基于用户名的子目录,我想知道哪里是最好的地方做这件事 我尝试添加ApplicationUser,但不确定如何正确添加,我知道完全错误的是以下内容Asp.net 如何在用户登录时为.net Core 2创建目录,asp.net,asp.net-core-mvc,Asp.net,Asp.net Core Mvc,我有一个要求,至少现在为.net核心网站创建基于用户名的子目录,我想知道哪里是最好的地方做这件事 我尝试添加ApplicationUser,但不确定如何正确添加,我知道完全错误的是以下内容 using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Hosting; using System.IO; namespace BRSCRM.Models { // Add profile data for applicati
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Hosting;
using System.IO;
namespace BRSCRM.Models
{
// Add profile data for application users by adding properties to the ApplicationUser class
public class ApplicationUser : IdentityUser
{
private IHostingEnvironment hostingEnvironment;
public string HomeDir { get; set; }
HomeDir=HostingEnvironment.WebRootPath + UserName;
string path = this.hostingEnvironment.WebRootPath + "\\uploads\\" + UserName;
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
}
我希望文档更好,他们似乎有很多入门材料,但是当你尝试做一些没有涉及的事情时,很难找到帮助
我想做的是为会员上传SupportFileUpload
我想我越来越接近了,但我现在得到了这个错误
'Microsoft.AspNetCore.Hosting.IHostingEnvironment'. Model bound complex types must not be abstract or value types and must have a parameterless constructor Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.CreateModel(ModelBindingContext
我似乎无法阅读IHostingEnvironment webrootpath,太令人沮丧了
我将代码移动到AccountController.cs中的注册操作中
这就是我到目前为止所拥有的
if (result.Succeeded)
{
await _userManager.AddToRoleAsync(user, "Member");
_logger.LogInformation("User created a new account with password.");
//Add code here to create directory....
string webRootPath = _hostingEnvironment.WebRootPath;
string contentRootPath = _hostingEnvironment.ContentRootPath;
var userId = User.FindFirstValue(ClaimTypes.Email);
string path = webRootPath + "\\uploads\\" + userId;
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
删除了环境的代码,因为它不起作用。无论如何,我尝试在本地系统上添加一个目录,发现我在索赔字段中没有得到任何东西。不知道如何从中获取用户名、电子邮件或其他内容,这让我发疯,请有人在这里提供建议 当用户上载文件时,只需检查文件夹是否存在,如果不存在,则在保存文件之前创建文件夹,这是否符合您的要求 例如,如果您的操作方法(假设MVC)如下所示:
[HttpPost(“上传文件”)]
公共异步任务发布(列表文件)
{
long size=files.Sum(f=>f.Length);
//临时位置中文件的完整路径
var filePath=Path.GetTempFileName();
foreach(文件中的var formFile)
{
如果(formFile.Length>0)
{
使用(var stream=newfilestream(filePath,FileMode.Create))
{
等待formFile.CopyToAsync(流);
}
}
}
//处理上载的文件
//未经验证,请勿依赖或信任FileName属性。
返回Ok(新的{count=files.count,size,filePath});
}
您可以简单地使用自己的路径来代替
var filePath=Path.GetTempFileName()代码>
并在保存前检查它是否存在/根据需要创建它 该代码1)语法错误,2)意识形态错误。
1) 以下代码必须采用某种方法,而不是在模型类定义中
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
2) MVC的主要思想是分离模型定义(M)、业务逻辑(controllerC)和表示(viewV)。因此,代码的一部分应该位于首先需要文件夹的某个控制器中(例如AccountController
),并从(例如)[HttpPost]Register
操作调用
private void SetUserFolder(ApplicationUser user)
{
IHostingEnvironment hostingEnvironment = /*getEnv()*/;
user.HomeDir = HostingEnvironment.WebRootPath + user.UserName;
string path = this.hostingEnvironment.WebRootPath + "\\uploads\\" + UserName;
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
}
不,因为我需要跟踪谁在上传文件,并且我的过程要求文件驻留在自己的目录中,所以我真的很奇怪,我在任何地方都找不到一个可以用于此项目的示例,这不是因为我已经找了好几天没有尝试过。你在使用MVC吗?您可以从控制器上当前登录用户的“user”属性中获取ID和name等用户属性。IE:var userId=User.FindFirstValue(ClaimTypes.NameIdentifier);然后,您可以使用它来构建文件夹路径,如string path=this.hostingEnvironment.WebRootPath+“\\uploads\\”+userid;如果文件夹不存在,则创建该文件夹。这似乎满足了您的要求。如果我能让实现正常工作,这就是我遇到的问题,我尝试先将其添加到控制器中,但我无法确定如何使其正常工作,因此我正在寻求帮助。哎哟,我想将其添加到帐户控制器中,但我也不确定该如何操作。。我会继续努力的。为什么需要检查目录是否存在?Directory.CreateDirectory在内部执行此操作。
private void SetUserFolder(ApplicationUser user)
{
IHostingEnvironment hostingEnvironment = /*getEnv()*/;
user.HomeDir = HostingEnvironment.WebRootPath + user.UserName;
string path = this.hostingEnvironment.WebRootPath + "\\uploads\\" + UserName;
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
}