我正在使用 ASP.NET Core 2.2 并且我正在使用模型绑定来上传文件。
这是我的UserViewModel
public class UserViewModel
{
[Required(ErrorMessage = "Please select a file.")]
[DataType(DataType.Upload)]
public IFormFile Photo { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的视图
@model UserViewModel
<form method="post"
asp-action="UploadPhoto"
asp-controller="TestFileUpload"
enctype="multipart/form-data">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input asp-for="Photo" />
<span asp-validation-for="Photo" class="text-danger"></span>
<input type="submit" value="Upload"/>
</form>
Run Code Online (Sandbox Code Playgroud)
最后这是MyController
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> UploadPhoto(UserViewModel userViewModel)
{
if (ModelState.IsValid)
{
var formFile = userViewModel.Photo;
if (formFile == null || formFile.Length == 0)
{
ModelState.AddModelError("", "Uploaded file is empty or …
Run Code Online (Sandbox Code Playgroud) 我将 ASP.Net Core 2.2 和 MediatR 框架/库用于查询对象。当我运行程序时,我遇到了这个异常:
InvalidOperationException:找不到 MediatR.IRequestHandler
2[Store.Core.Queries.GetProductTypesQuery,System.Collections.Generic.IEnumerable
1[Store.Core.DomainModels.ProductType]]类型请求的处理程序。向容器注册您的处理程序。
我将这些打包添加到我的 Store 项目(主项目)中
1-MediatR 7.0.0
2- MediatR.Extensions.Microsoft.DependencyInjection
这是我的Startup.cs
services.AddMediatR(typeof(Startup));
Run Code Online (Sandbox Code Playgroud)
所以这是我的查询(位于名为“Store.Core”的项目中)
namespace Store.Core.Queries.Products
{
public class GetProductTypesQuery : IRequest<IEnumerable<ProductType>> { }
}
Run Code Online (Sandbox Code Playgroud)
这是我的QueryHandler(位于另一个名为“Store.Data”的项目中)
namespace Data.Queries.Products
{
public class GetProductTypesQueryHandler : IRequestHandler<GetProductTypesQuery, IEnumerable<ProductType>>
{
private ApplicationDbContext _context;
public GetProductTypesQueryHandler(ApplicationDbContext context)
{
_context = context;
}
public async Task<IEnumerable<ProductType>> Handle(GetProductTypesQuery request, CancellationToken cancellationToken)
{
return await _context.ProductType.OrderBy(p => p.ProductTypeID).ToListAsync();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我使用 MediatR 的控制器
namespace Store.Controllers …
Run Code Online (Sandbox Code Playgroud) 我使用 Typeorm (v8.0.2) 和 Nestjs(v8) 以及 Nodejs(v16)。我的问题是当我创建一本书时 Typeorm 不返回生成的书籍 ID
这是Book.entity
@Entity()
export class Book {
@PrimaryGeneratedColumn('increment')
id: number;
@Column()
title: string;
@Column()
author: string;
}
Run Code Online (Sandbox Code Playgroud)
这是book.service
async createBook(createBookDto: CreateBookDto): Promise<Book> {
const book = await this.bookRepository.create(createBookDto)
await this.bookRepository.save(createBookDto)
return book
}
Run Code Online (Sandbox Code Playgroud)
当我使用邮递员并创建一本书时,它只会返回
{
title: "example"
author: "foo"
}
Run Code Online (Sandbox Code Playgroud)
生成的书籍 ID 丢失
asp.net ×2
asp.net-core ×2
c# ×2
ef-core-2.1 ×1
javascript ×1
mediatr ×1
node.js ×1
typeorm ×1