返回帶有額外字符串的實體模型屬性

我有一個使用Dapper作為ORM的ASP.NET Core 8 Web API。

這是我的實體類:

namespace Server.Entities
{
    public class Post
    {
        public int Id { get; set; } 
        public string Title { get; set; } 
        public string Slug { get; set; } 
        public int CategoryId { get; set; } 
        public bool Bookmarked = false;
        public bool Liked = false;
        public bool Premium { get; set; }
        public string BriefText { get; set; } 
        public string Text { get; set; }
        public string CoverImage { get; set; } 
        public int ReadingTime { get; set; }
        public Collection<string> Tags = new Collection<string>();
        public int Author { get; set; } 
        public Collection<Post> Relateds = new Collection<Post>();
        public Collection<Comment> Comments = new Collection<Comment>();
    }

    public class PostDTO
    {
        public string Title { get; set; }
        public string Slug { get; set; }
        public string CoverImage { get; set; }
        public string Text { get; set; }
    }
}

這是我的控制器:

 [HttpGet]
 public async Task<ActionResult> GetAllPosts()
 {
    PostDTO[] posts = await _dbContext.Post.GetAllAsync();
    return Ok(posts);
 }

我的服務:

 public async Task<PostDTO[]> GetAllAsync()
 {
    string query = "SELECT * FROM Posts";
    IEnumerable<PostDTO> posts = await _connection.QueryAsync<PostDTO>(query);
    return posts.ToArray();
 }

如您所見,我將PostDTO返回給客戶端,CoverImage屬性是一個沒有域名的圖像URL,如下所示:

 /uploads/coverImage/2024/7/10/1723272284733-718853888.jpg

為了在前端使用此url,我必須在url之前添加域。在將其發送到客戶端之前,有什么方法可以自動將域與屬性組合在一起嗎?

我用了這個:

public class PostDTO
{
    public string Title { get; set; }
    public string Slug { get; set; }

    public string CoverImage { get; set; }
    public string CoverImageURL
    {
        get => "http://localhost:8000" + CoverImage;
        set => CoverImage = value;
    }
    public string Text { get; set; }
}

這樣我就有了一個完整的圖像URL,但我認為向客戶端返回額外的屬性不是一個好主意。

如果我修改main屬性CoverImage以返回完整的URL,如下所示:

public class PostDTO
{
    public string Title { get; set; }
    public string Slug { get; set; }

    public string CoverImage
    {
        get => "http://localhost:8000" + CoverImage;
        set => CoverImage = value;
    }
    public string Text { get; set; }
}

使用這個,我從Dapper得到一個錯誤:

info:Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[102]路由與{action=“GetAllPosts”,controller=“Post”}匹配。在控制器Server.Controllers.PostController(服務器)上執行簽名為System.Threading.Tasks.Task`1[Microsoft.AspNetCore.Mvc.ActionResult] GetAllPost()的控制器操作。堆棧溢出。重復24234次:

at Server.Entities.PostDTO.set_CoverImage(System.String)
at DynamicClass.Deserializedeb5d960-6b33-4fce-84f4-e8ee01aaac3a(System.Data.Common.DbDataReader)
at Dapper.SqlMapper+d__331[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext() at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1+AsyncStateMachineBox1[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[Dapper.SqlMapper+<QueryAsync>d__331[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], Dapper, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null]].ExecutionContextCallback(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1+AsyncStateMachineBox1[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e], Dapper.SqlMapper+d__331[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], Dapper, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext(System.Threading.Thread) at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1+AsyncStateMachineBox1[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e], Dapper.SqlMapper+<QueryAsync>d__331[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], Dapper, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext()
at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Runtime.CompilerServices.IAsyncStateMachineBox, Boolean)
at System.Threading.Tasks.Task.RunContinuations(System.Object)
at System.Threading.Tasks.Task.FinishSlow(Boolean)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(System.Threading.Tasks.Task ByRef, System.Threading.Thread)
at System.Threading.Tasks.ThreadPoolTaskScheduler.TryExecuteTaskInline(System.Threading.Tasks.Task, Boolean)
at System.Threading.Tasks.TaskScheduler.TryRunInline(System.Threading.Tasks.Task, Boolean)
at System.Threading.Tasks.TaskContinuation.InlineIfPossibleOrElseQueue(System.Threading.Tasks.Task, Boolean)
at System.Threading.Tasks.ContinueWithTaskContinuation.Run(System.Threading.Tasks.Task, Boolean)
at System.Threading.Tasks.Task.RunContinuations(System.Object)
at System.Threading.Tasks.Task1[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].TrySetResult(System.__Canon) at System.Threading.Tasks.UnwrapPromise1[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].TrySetFromTask(System.Threading.Tasks.Task, Boolean)
at System.Threading.Tasks.UnwrapPromise`1[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].ProcessInnerTask(System.Threading.Tasks.Task)
at System.Threading.Tasks.Task.RunContinuations(System.Object)
at System.Threading.Tasks.Task.FinishSlow(Boolean)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(System.Threading.Tasks.Task ByRef, System.Threading.Thread)
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading.PortableThreadPool+WorkerThread.WorkerThreadStart()

? 最佳回答:

此SO問題與導致此問題的CoverImage集方法調用本身有關。

為了解決這個問題,我建議你可以編寫一個新的屬性來創建完整的本地字符串。

更多詳細信息,您可以參考以下示例:

PostDTO:

public class PostDTO
{
    public string Title { get; set; }
    public string Slug { get; set; }

  
    private string _coverImage;

 
    public string CoverImage
    {
        get => _coverImage;
        set => _coverImage = value;
    }

     public string FullCoverImageURL
    {
        get => "http://localhost:8000" + _coverImage;
    }

    public string Text { get; set; }
}

Usage:

        var connectionString = @"xx";

        using var conn = new SqlConnection(connectionString);
        string query = "SELECT * FROM Posts";
        IEnumerable<PostDTO> posts =   conn.Query<PostDTO>(query);
        var result= posts.ToArray();

        return Ok(result);

Result:

主站蜘蛛池模板: 国产精品无码一区二区三区免费 | 无码免费一区二区三区免费播放| 一区二区免费电影| 亚洲AV无码一区二区三区电影| 区三区激情福利综合中文字幕在线一区亚洲视频1 | 自拍日韩亚洲一区在线| 中文字幕一区在线| 麻豆精品一区二区综合av| 亚洲AV一区二区三区四区| 亚洲一区二区三区国产精品无码| 国产91精品一区二区麻豆亚洲| 国产伦精品一区三区视频| 四虎成人精品一区二区免费网站| 国产伦精品一区二区三区女| 一区二区在线电影| 日本一区二区在线播放| 亚洲AV成人一区二区三区AV| 国产精品伦一区二区三级视频 | 久久久久人妻精品一区三寸| 亚洲va乱码一区二区三区| 亚洲av午夜福利精品一区| 亚洲av无码不卡一区二区三区| 国产成人一区二区三区在线| 午夜福利一区二区三区在线观看| 国产情侣一区二区三区| 精品国产毛片一区二区无码| 91久久精一区二区三区大全| 国产伦精品一区二区三区视频猫咪| 夜色阁亚洲一区二区三区| 一区二区三区日本电影| 久久久久国产一区二区三区| 精品国产亚洲一区二区三区| 亚洲av无码不卡一区二区三区 | 多人伦精品一区二区三区视频| 成人国产精品一区二区网站公司| 理论亚洲区美一区二区三区| 国产一区二区三区内射高清| 精品人妻少妇一区二区三区在线 | 精品91一区二区三区| 自慰无码一区二区三区| 久久久无码精品国产一区|