EF Core 1.0 with ASP.NET Core 1.0 MVC WebApi error 404

asp.net-core asp.net-web-api entity-framework-core iis-7.5

Question

I have a small application that uses EF Core 1.0 with ASP.NET Core 1.0 MVC WebApi, is working perfectly in kestrel self-hosting scenarios (IIS Express) .When i publish on IIS 7.0, i get 404 error when i call method use EF Core, but when i call method that does not use EF Core it work perfectly.

[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();
    }
}

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

} }

What is the configuration for using EF Core 1.0 with ASP.NET Core 1.0 MVC WebApi on IIS Server.

Thanks for your help. You would find at this url an application answering the same criterion that will give a 404 error after being pulse on IIS 7.0

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

1
0
11/14/2016 4:48:03 PM

Accepted Answer

The problem is that when I use a connection string with secure integer and deploy it on an IIS 7 on my workstation IIS uses the name of my computer and not mine as the identifier for Entityframework core.

0
4/13/2017 6:58:45 PM

Popular Answer

The best practice to use routing is to define the route in a separate route attribute.

So, your code should look like this:

[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);
        }
    }
}

Give it a try.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow