web*_*183 2 asp.net asp.net-core-mvc
我有一个需求,至少现在是,基于 .NET Core 网站的用户名创建一个子目录。哪里是最好的地方?
我尝试添加 ApplicationUser ,但不确定如何正确添加它。我所拥有的,我知道是完全错误的,如下。
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);
}
Run Code Online (Sandbox Code Playgroud)
我希望文档更好。似乎他们有很多入门资料,但是当您尝试做一些未涵盖的内容时,很难找到帮助。
我正在尝试做的是支持成员的文件上传。
我想我越来越近了,但我现在收到此错误:>“Microsoft.AspNetCore.Hosting.IHostingEnvironment”。模型绑定复杂类型不能是抽象类型或值类型,并且必须具有无参数构造函数 Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.CreateModel(ModelBindingContext
我似乎无法读取 IHostingEnvironment webrootpath。这太令人沮丧了!!
我将代码移到文件AccountController.cs 中的 Register 操作中...
这是我到目前为止..
if (result.Succeeded)
{
await _userManager.AddToRoleAsync(user, "Member");
_logger.LogInformation("User created a new account with password.");
// Add code here to create a 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);
Run Code Online (Sandbox Code Playgroud)
我删除了环境的代码,因为它无论如何都不起作用。我试图在我的本地系统上添加一个目录,但我发现我在声明字段中没有得到任何东西。我不确定如何从中获取用户名、电子邮件或其他任何内容。我该怎么办?
代码是 1) 语法和 2) 意识形态不正确。
以下代码必须在某个方法中,而不是在模型class定义中
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
Run Code Online (Sandbox Code Playgroud)
MVC的主要思想是将模型定义(M)、业务逻辑(控制器C)和表示(视图V)分开。因此,代码的一部分应该位于首先需要文件夹(例如,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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8658 次 |
| 最近记录: |