带有 ASP.NET Core 1.0 MVC WebApi 错误 404 的 EF Core 1.0

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 的小应用程序,它在 kestrel 自托管场景 (IIS Express) 中完美运行。当我在 IIS 7.0 上发布时,我得到 404当我调用方法使用 EF Core 时出错,但是当我调用不使用 EF Core 的方法时它工作正常。

[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.NETCore 1.0 MVC WebApi 的配置是什么。

感谢您的帮助。您会在此 url 中找到一个应用程序,该应用程序满足相同的标准,在 IIS 7.0

上运行后会出现 404 错误

https://github.com/FabianGosebrink/ASPNET-Core-Entity-Framework-Core

is working perfectly in kestrel self-hosting scenarios (IIS Express) .When i publish on IIS 7.0, i get 404 error

这看起来像是与您的发布过程相关的问题,而不是与 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 核心的标识符。