C# 具有实体框架的Blazor webassembly失败,返回“0”;不支持ContentType;支持的类型为';application/json'&引用;
这是一个简单的CRUD应用程序,我正在使用它来了解如何工作。我以前使用过实体框架,但对它的工作原理知之甚少。此应用程序有一个数据库,其中有一个表,C# 具有实体框架的Blazor webassembly失败,返回“0”;不支持ContentType;支持的类型为';application/json'&引用;,c#,entity-framework,blazor,webassembly,C#,Entity Framework,Blazor,Webassembly,这是一个简单的CRUD应用程序,我正在使用它来了解如何工作。我以前使用过实体框架,但对它的工作原理知之甚少。此应用程序有一个数据库,其中有一个表,Employee,有6个常用类型列。我下载了在System.Text.Json之前编写的同一个应用程序的工作副本,因此数据库连接正常 GetEmployee.razor中的调用是: @code { private Employee[] empList; protected override async Task OnInitialize
Employee
,有6个常用类型列。我下载了在System.Text.Json
之前编写的同一个应用程序的工作副本,因此数据库连接正常
GetEmployee.razor
中的调用是:
@code {
private Employee[] empList;
protected override async Task OnInitializedAsync()
{
try
{
empList = await Http.GetFromJsonAsync<Employee[]>("/api/Employee/Index");
}
catch (Exception ex)
{
string foo = ex.ToString();
}
}
}
namespace Clean.Server.Api
{
public partial class ManagementContext : DbContext
{
public ManagementContext()
{
}
public ManagementContext(DbContextOptions<ManagementContext> options)
: base(options)
{
}
public virtual DbSet<Employee> Employee { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
optionsBuilder.UseSqlServer("Server=MtLyell\\SQLEXPRESS;Database=Management;Integrated Security=True;");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Employee>(entity =>
{
entity.Property(e => e.Designation)
.HasMaxLength(100)
.IsUnicode(false);
entity.Property(e => e.Email)
.HasMaxLength(20)
.IsUnicode(false);
entity.Property(e => e.Location)
.IsRequired()
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.Name)
.IsRequired()
.HasMaxLength(100)
.IsUnicode(false);
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
}
amespace Clean.Server.Api
{
public partial class Employee
{
public long EmployeeId { get; set; }
public string Name { get; set; }
public string Designation { get; set; }
public string Email { get; set; }
public string Location { get; set; }
public long PhoneNumber { get; set; }
}
}
namespace Clean.Server.Api
{
public interface IEmployeAccessLayer
{
IEnumerable<Employee> GetAllEmployees();
void AddEmployee(Employee employee);
void UpdateEmployee(Employee employee);
Employee GetEmployeeData(long id);
void DeleteEmployee(long id);
}
public class EmployeAccessLayer : IEmployeAccessLayer
{
private ManagementContext _context;
public EmployeAccessLayer(ManagementContext context)
{
_context = context;
}
//To Get all employees details
public IEnumerable<Employee> GetAllEmployees()
{
try
{
return _context.Employee.ToList();
}
catch(Exception ex)
{
throw;
}
}
//To Add new employee record
public void AddEmployee(Employee employee)
{
try
{
_context.Employee.Add(employee);
_context.SaveChanges();
}
catch
{
throw;
}
}
//To Update the records of a particluar employee
public void UpdateEmployee(Employee employee)
{
try
{
_context.Entry(employee).State = EntityState.Modified;
_context.SaveChanges();
}
catch
{
throw;
}
}
//Get the details of a particular employee
public Employee GetEmployeeData(long id)
{
try
{
Employee employee = _context.Employee.Find(id);
return employee;
}
catch
{
throw;
}
}
//To Delete the record of a particular employee
public void DeleteEmployee(long id)
{
try
{
Employee emp = _context.Employee.Find(id);
_context.Employee.Remove(emp);
_context.SaveChanges();
}
catch
{
throw;
}
}
}
}
namespace Clean.Server.Controllers
{
public class EmployeeController : ControllerBase
{
IEmployeAccessLayer _employee;
public EmployeeController(IEmployeAccessLayer employee)
{
_employee = employee;
}
[HttpGet]
[Route("api/Employee/Index")]
public IEnumerable<Employee> Index()
{
return _employee.GetAllEmployees();
}
[HttpPost]
[Route("api/Employee/Create")]
public void Create([FromBody] Employee employee)
{
if (ModelState.IsValid)
this._employee.AddEmployee(employee);
}
[HttpGet]
[Route("api/Employee/Details/{id}")]
public Employee Details(int id)
{
return _employee.GetEmployeeData(id);
}
[HttpPut]
[Route("api/Employee/Edit")]
public void Edit([FromBody]Employee employee)
{
if (ModelState.IsValid)
this._employee.UpdateEmployee(employee);
}
[HttpDelete]
[Route("api/Employee/Delete/{id}")]
public void Delete(int id)
{
_employee.DeleteEmployee(id);
}
}
}
EmployeeAccessLayer.cs
中的序列化是否是进行此转换的正确想法和位置
如果是这样,我如何将其转换为正确的类型
或者EF中是否有适合我的设置 在Startup.cs中,我需要注册ManagementContext和IEEmployeAccessLayer,我做到了:
public static string Connection { get; set; }
在razor文件中似乎有效的json命令(最小测试)有:
GetFromJsonAsync,
PutAsJsonAsync,
DeleteAsync,
在Startup.cs中,我需要注册ManagementContext和IEEmployeAccessLayer,我做到了:
public static string Connection { get; set; }
在razor文件中似乎有效的json命令(最小测试)有:
GetFromJsonAsync,
PutAsJsonAsync,
DeleteAsync,
PostAsJsonAsync测试
“/api/Employee”
端点。在浏览器中删除完整的URL。您可能有一个以HTML形式返回的服务器错误。看见你在什么地方不需要员工ID吗?我花了很多时间试图找到如何接受这些编辑。从来没有找到ti。昨天我做了一些更改,我猜是一次更改太多了,并破坏了应用程序,所以我建立了一个新的名为Clean的应用程序。之后,错误消息更改为:“System.Net.Http.HttpRequestException:Response status code不表示成功:500”我在api调用中使用斜杠和不使用斜杠进行了尝试,您说得对,它需要一个EmployeeId。但是“/api/Employee/Index”)仍然无法工作。请测试“/api/Employee”
端点。在浏览器中删除完整的URL。您可能有一个以HTML形式返回的服务器错误。看见你在什么地方不需要员工ID吗?我花了很多时间试图找到如何接受这些编辑。从来没有找到ti。昨天我做了一些更改,我猜是一次更改太多了,并破坏了应用程序,所以我建立了一个新的名为Clean的应用程序。之后,错误消息更改为:“System.Net.Http.HttpRequestException:Response status code不表示成功:500”我在api调用中使用斜杠和不使用斜杠进行了尝试,您说得对,它需要一个EmployeeId。但是“/api/Employee/Index”)仍然不起作用。我将项目原样上传到GitHub:我希望它对某人有用。我将项目原样上传到GitHub:我希望它对某人有用。
Connection = Configuration.GetConnectionString("EmployeeDatabase");
services.AddDbContext<ManagementContext>(options => options.UseSqlServer(Connection));
services.AddScoped<IEmployeAccessLayer, EmployeAccessLayer>();
optionsBuilder.UseSqlServer(Clean.Server.Startup.Connection);