[英]EF Core 1.0 with ASP.NET Core 1.0 MVC WebApi error 404
我有一個使用EF Core 1.0和ASP.NET Core 1.0 MVC WebApi的小型應用程序,在紅est自托管方案(IIS Express)中運行良好。當我在IIS 7.0上發布時,調用方法使用EF時出現404錯誤核心,但是當我調用不使用EF核心的方法時,它可以完美地工作。
[Route("api/[controller]")]
public class MyModelController : Controller
{
private readonly IExampleRepository _exampleRepository;
public MyModelController(IExampleRepository exampleRepository)
{
_exampleRepository = exampleRepository;
}
[HttpGet("GetTestAll")] //RUN PERFECTLY ON IIS
public IEnumerable<string> GetTest()
{
return new string[] { "value1", "value2", "value3", "value4" };
}
// GET: api/mymodel
[HttpGet("", Name = "GetAll")] //ERROR 404 ON IIS
public IActionResult Get()
{
try
{
return Ok(_exampleRepository.GetAll().Select(x => Mapper.Map<MyModelViewModel>(x)));
}
catch (Exception exception)
{
//logg exception or do anything with it
return StatusCode((int)HttpStatusCode.InternalServerError);
}
}
...
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);
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
var configurationSection = Configuration.GetSection("ConnectionStrings:DefaultConnection");
services.AddDbContext<DataBaseContext>(options => options.UseSqlServer(configurationSection.Value));
// Add framework services.
services.AddMvc();
services.AddScoped<IExampleRepository, ExampleRepository>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
Mapper.Initialize(config =>
{
config.CreateMap<MyModel, MyModelViewModel>().ReverseMap();
});
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseMvc();
}
}
提取project.json
"Microsoft.Extensions.Configuration.CommandLine": "1.0.0",
"Microsoft.ApplicationInsights.AspNetCore": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.AspNetCore.Server.WebListener": "1.0.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"Microsoft.AspNetCore.StaticFiles": "1.0.0",
"Microsoft.EntityFrameworkCore": "1.0.0",
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
"Microsoft.EntityFrameworkCore.Tools": {
"version": "1.0.0-preview2-final",
"type": "build"
},
"AutoMapper": "5.0.0"
}}
在IIS服務器上將EF Core 1.0與ASP.NET Core 1.0 MVC WebApi一起使用的配置是什么。
謝謝你的幫助。 您將在此URL上找到一個應用程序,該應用程序回答了在IIS 7.0上被脈沖后將給出404錯誤的相同條件
https://github.com/FabianGosebrink/ASPNET-Core-Entity-Framework-Core
在紅est自托管方案(IIS Express)中運行良好。當我在IIS 7.0上發布時,出現404錯誤
它看起來像是與您的發布過程有關的問題,而不是與EF有關的問題,因為它在調試時起作用。
_exampleRepository.GetAll()
或Automapper
可能都需要一個已發布項目中缺少的文件。
在project.json
文件中,您應該包括要在publish
過程中復制的所有內容的列表。 例如我的看起來像這樣
"publishOptions": {
"include": [
"wwwroot",
"web.config",
"appsettings.Development.json",
"appsettings.Production.json",
"Views",
"Resources/*"
]
}
使用路由的最佳實踐是在單獨的路由屬性中定義路由。
因此,您的代碼應如下所示:
[Route("api/[controller]")]
public class MyModelController : Controller
{
private readonly IExampleRepository _exampleRepository;
public MyModelController(IExampleRepository exampleRepository)
{
_exampleRepository = exampleRepository;
}
[HttpGet] //RUN PERFECTLY ON IIS
[Route("GetTestAll")]
public IEnumerable<string> GetTest()
{
return new string[] { "value1", "value2", "value3", "value4" };
}
// GET: api/mymodel
[HttpGet] //ERROR 404 ON IIS
[Route("GetAll")]
public IActionResult Get()
{
try
{
return Ok(_exampleRepository.GetAll().Select(x => Mapper.Map<MyModelViewModel>(x)));
}
catch (Exception exception)
{
//log exception or do anything with it
return StatusCode((int)HttpStatusCode.InternalServerError);
}
}
}
試試看。
問題是,當我使用帶有安全整數的連接字符串並將其部署在工作站上的IIS 7上時,IIS會使用計算機的名稱,而不是將其用作Entityframework核心的標識符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.