.Net core web api 上传图片代码 。 AutoMapper映射注入。sql suger数据库依赖注入

 目录

(1).Net core web api 上传图片代码

(2)AutoMapper映射注入

(3)sql suger数据库依赖注入

.Net core web api 上传图片代码

/// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> FileSave(IFormFile file)
        {
            string fileName = "";
            var saveName = "";
            var filePath = "";
            try
            {
                //获取Form提交的文件
                if (file != null)
                {
                    if (file.Length > 0)
                    {
                        var now = DateTime.Now;
                        //文件存储路径
                        filePath = string.Format("wwwroot/Uploads/{0}/{1}/{2}/", now.ToString("yyyy"), now.ToString("yyyyMM"), now.ToString("yyyyMMdd"));
                        //获取当前web目录
                        var webRootPath = _webHostEnvironment.ContentRootPath;
                        //保存目录不存在就创建这个目录
                        if (!Directory.Exists(webRootPath + filePath))
                        {
                            Directory.CreateDirectory(webRootPath + filePath);
                        }
                        #region  图片文件的条件判断
         

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,以下是在 .NET 6 ASP.NET Core Web API 中使用 AutoMap 的初始化和帮助类。 首先,你需要在你的项目中添加 AutoMap 的 NuGet 包。在 Visual Studio 中,右键点击项目,选择“Manage NuGet Packages”,然后搜索 AutoMap 并安装。 接下来,你需要创建一个帮助类来帮助你初始化 AutoMap。这个类可以是一个静态类,包含一个静态的初始化方法。这个初始化方法将会注册你的 Mapper 配置,以便在应用程序启动时自动执行。 ```csharp using AutoMapper; public static class AutoMapperConfig { public static void Initialize() { MapperConfiguration config = new MapperConfiguration(cfg => { // 在这里进行你的 Mapper 配置 cfg.CreateMap<SourceClass, DestinationClass>(); }); IMapper mapper = config.CreateMapper(); Mapper = mapper; } public static IMapper Mapper { get; private set; } } ``` 在你的 Startup.cs 文件中,你可以在 ConfigureServices 方法中调用这个初始化方法: ```csharp public void ConfigureServices(IServiceCollection services) { // 其他配置... AutoMapperConfig.Initialize(); } ``` 现在,你可以在你的控制器或其他服务中注入 IMapper 接口,使用 AutoMap 进行对象映射了。 ```csharp using AutoMapper; public class MyController : ControllerBase { private readonly IMapper _mapper; public MyController(IMapper mapper) { _mapper = mapper; } public IActionResult MyAction() { SourceClass source = new SourceClass(); DestinationClass destination = _mapper.Map<DestinationClass>(source); // 其他代码... } } ``` 这样,你就可以在 .NET 6 ASP.NET Core Web API 中使用 AutoMap 了。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Carson fang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值