3

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.

2
  • The problem is not in your code but in IIS settings. Check your IIS settings and if they support .Net Core. Here is microsoft documentation on setting it up: learn.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/… Commented Sep 20, 2019 at 19:09
  • First, try to move app.UseDeveloperExceptionPage(); outside if statement, and share us the error response. Do you connect local database? Try to run your web site under your account by change identity in your iis applicaton pools. Commented Sep 23, 2019 at 8:16

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

not working. i think problem is about my dbconection. the default value api without using database work nice.
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.