A. Jafarzadeh
A. Jafarzadeh

Reputation: 41

asp.net core web api work on iis express but not working in iis on windows

i am new in asp.net core. i use asp.net core 2.1. i have two controller. a default valuecontroller that doesn't have db connection and customerController that have sqlserver db connection. when i run my project on iis express everything is good but when i publish my project and use windows iis value api work nice but my customer api that have a sqlserver connection doesn't work.

appsettings calss:

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "DefaultConnection": "Data Source=AHAD;Initial Catalog=mydb;Integrated Security=True"
  }
}

Startup class:

namespace SalesApp
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            services.AddDbContext<MyDbContext>(Options =>
            {
                Options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            });
        }

        // 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,
    //MyDbContext db)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseMvc();
            //loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            //loggerFactory.AddDebug();

            //db.Database.Migrate();

            //app.UseMvc();
        }
    }
}

MyDbContext class:

public class MyDbContext : DbContext
    {

        public MyDbContext()
        {

        }

        public MyDbContext(DbContextOptions<MyDbContext> options)
            : base(options)
        {

        }

        //public DbSet<CUSTOMER> customers { get; set; }

        public DbSet<CUSTOMER> Customer { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<CUSTOMER>(entity =>
            {
                entity.Property(e => e.C_Code).HasMaxLength(5);
                entity.Property(e => e.C_Name).HasMaxLength(60);
                entity.Property(e => e.C_Code_C).HasMaxLength(12);
            });
        }

    }

CustomerController Class:

[Route("api/[controller]")]
    [ApiController]
    public class CustomerController : ControllerBase
    {
        MyDbContext _context;

        public CustomerController(MyDbContext context)
        {
            _context = context;
        }

        [HttpGet]
        public IActionResult GetCustomers()
        {
            return new ObjectResult(_context.Customer);
        }

    }

values api: enter image description here

customer api: enter image description here

it confused me 2 weeks.

Upvotes: 3

Views: 9172

Answers (2)

Sinan
Sinan

Reputation: 1

Using sql server auth worked for me. I use connection string like this:

Server=.;
Database=myDB;
User Id=sa2;
Password=myPass;

Upvotes: 0

Dennis VW
Dennis VW

Reputation: 3217

You will need to have IIS set up in order to get it to work correctly.

enter image description here

You also need to ensure you are using the .NET Core Windows Server Hosting Bundle

Then:

Restart the system or execute net stop was /y, followed by net start w3svc from a command shell. Restarting IIS picks up a change to the system PATH, which is an environment variable, made by the installer.

After that open the command prompt as Administrator and type:

C:\Windows\System32> iisreset

Then publish the app to a folder and open the command prompt there. Run the application by typing

C:\Temp\publish> dotnet YourApplicationName.dll

You can now go to the browser and type in http://localhost:port/ and it will display your .Net Core app.

Upvotes: 1

Related Questions