【发布时间】:2017-01-12 10:00:50
【问题描述】:
我对 .Net Core 和 Entity Framework 都是全新的。我正在尝试通过遵循一些教程来学习这两种技术,但我很挣扎。我的 Web API 项目有两个控制器 - 向导生成的 ValuesController.cs 是在创建新的 Core Web API 项目时创建的,我添加的第二个控制器称为 FilesController.cs。
我的 appsettings.json:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"ConnectionStrings": {
" \"PropWorxConnection\"": "server=a.b.com;user id=propworx;persistsecurityinfo=True;database=abcde;password=12345"
}
}
我的 Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using MySQL.Data.EntityFrameworkCore.Extensions;
namespace PropWorxAPI
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddDbContext<Models.PropWorxDbContext>(options =>
options.UseMySQL(Configuration.GetConnectionString("PropWorxConnection")));
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMvc();
}
}
}
一个简单的模型:
namespace PropWorxAPI.Models
{
[Table("files")]
public partial class File
{
public int ID { get; set; }
public string FileNum { get; set; }
}
}
我的背景:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using MySQL.Data.EntityFrameworkCore.Extensions;
namespace PropWorxAPI.Models
{
public class PropWorxDbContext : DbContext
{
private readonly string _connectionString;
public PropWorxDbContext(string connectionString)
{
_connectionString = connectionString;
}
public PropWorxDbContext(DbContextOptions<PropWorxDbContext> options) : base(options)
{
}
public DbSet<Models.File> Files { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Models.File>().ToTable("Files");
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseMySQL(_connectionString);
base.OnConfiguring(optionsBuilder);
}
}
}
和我的控制器:
using Microsoft.AspNetCore.Mvc;
using PropWorxAPI.Models;
namespace PropWorxAPI.Controllers
{
[Route("api/[controller]")]
public class FilesController : Controller
{
private readonly PropWorxDbContext _context;
public FilesController(PropWorxDbContext context)
{
_context = context;
}
[HttpGet]
public string Get()
{
return "Controller Working!!!";
}
}
}
现在,当我运行它 (F5) 时,它会打开 Edge 并转到 http://localhost:51932/api/values。这将显示以下输出:
["value1","value2"]
所以它默认为 ValuesController (我该如何改变它???)。无论如何,重要的是自动生成的 ValuesController 有效。我现在手动更改 URL 以指向我创建的 FilesController,即
http://localhost:51932/api/files
...我明白了:
HTTP 500 错误 奇怪...网站无法显示此页面
现在,如果我从 FilesController 中删除上下文参数 - 即我来自:
public FilesController(PropWorxDbContext context)
{
_context = context;
}
到
public FilesController()
{
}
然后它可以正常工作并正确显示:
这是文件控制器
任何想法为什么?谢谢,对于过于冗长的帖子感到抱歉...
【问题讨论】:
标签: asp.net-core asp.net-core-mvc asp.net-core-webapi