我有一個使用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.AsyncTaskMethodBuilder
1+AsyncStateMachineBox1[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[Dapper.SqlMapper+<QueryAsync>d__33
1[[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+AsyncStateMachineBox
1[[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.AsyncTaskMethodBuilder
1+AsyncStateMachineBox1[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e], Dapper.SqlMapper+<QueryAsync>d__33
1[[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.UnwrapPromise
1[[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:
Usage:
Result: