0

我有一个小的应用程序,使用EF核心1.0与ASP.NET核心1.0 MVC WebApi,完美工作在茶隼自托管场景(IIS Express)。当时我在IIS 7.0上发布时,当我调用方法使用EF Core时出现404错误,但是当我调用不使用EF Core的方法时,它完美地工作。EF核心1.0与ASP.NET核心1.0 MVC WebApi错误404

[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" 

}}

什么是使用EF核心1.0 ASP.NET配置核心1.0 MVC在IIS服务器上的WebApi。

感谢您的帮助。你会发现在这个网址的应用程序回答同样的标准在IIS 7.0上

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

+1

你怎么称呼它? 'GET example.com/api/mymodel'? – Tseng

+0

在IIS上,我使用Postman和这个URL:http:// localhost:8088/api/mymodel –

回答

0

问题是,当我使用具有安全整数的连接字符串并将其部署在工作站上的IIS 7上时 IIS使用我的计算机的名称,而不是我作为Entityframework核心的标识符。

0

是脉冲后,将给予404错误在红隼自托管场景(IIS快递)。当工作完美我发布在IIS 7.0上,我得到404错误

看起来与您的发布过程,而不是EF,因为它的工作,当你调试问题。

您的_exampleRepository.GetAll()Automapper可能需要您发布的项目中缺少的文件。

在你的project.json文件中,你应该包含你想在你的publish过程中复制的所有内容的列表。例如,我的看起来像这样

"publishOptions": { 
    "include": [ 
     "wwwroot", 
     "web.config", 
     "appsettings.Development.json", 
     "appsettings.Production.json", 
     "Views", 
     "Resources/*" 
    ] 
} 
+0

感谢您的回复,但是我并没有在project.json的publishOption部分下面看到丢失的文件:“publishOptions”: { “包括”:[ “wwwroot的”, “视图”, “区域/ ** /浏览次数”, “**/* CSHTML。”, “appsettings.json”, “appsettings.Production。 json“, ”网页。config“ ] }, –

+0

即使运行已发布的网站版本,您也应该激活详细的错误功能[https://docs.microsoft.com/en-us/aspnet/core/fundamentals/hosting] (https://docs.microsoft.com/en-us/aspnet/core/fundamentals/hosting)。而不是404你会知道发生了什么,我相信你的HTTP请求会触及你的控制器的方法,但404发生在 –

+0

而不是托管它在IIS上,你也可以尝试发布到一个文件夹,并在你的机器上本地运行它。'dotnet publish ./src/YourProject --runtime win-x64 --configuration Release'然后转到'./src/ YourProject/bin/Release/netcoreapp1.0/publish /'并运行'dotnet YourProject.dll' –

0

使用路由的最佳实践是在单独的路由属性中定义路由。

所以,你的代码应该是这样的:

[Route("api/[controller]")] 
public class MyModelController : Controller 
{ 
    private readonly IExampleRepository _exampleRepository; 

    public MyModelController(IExampleRepository exampleRepository) 
    { 
     _exampleRepository = exampleRepository; 
    } 

    [HttpGet] //RUN PERFECTLY ON IIS 
    [Routue("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); 
     } 
    } 
} 

试试看吧。