ASP.NET Core Web API在IIS Express上可工作,但在Windows上的IIS上无法工作

3
我可以帮您翻译这段内容。该段文字涉及IT技术,讲述ASP.NET Core的使用问题。其中包括两个控制器,一个默认值控制器没有数据库连接,另一个客户控制器有SQL Server数据库连接。在使用IIS Express运行项目时一切正常,但是当发布并使用Windows IIS后,只有默认值API能够正常工作,具有SQL Server连接的客户API无法正常工作。
以下是appsettings类:
{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "DefaultConnection": "Data Source=AHAD;Initial Catalog=mydb;Integrated Security=True"
  }
}

启动类:

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类:

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

    }

客户控制器类:

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

    }

值API: 在此输入图像描述

客户API: 在此输入图像描述

这让我迷惑了两周。


问题不在你的代码上,而是在IIS设置中。检查你的IIS设置,看它们是否支持.Net Core。 这里有微软关于设置的文档: https://learn.microsoft.com/zh-cn/aspnet/core/host-and-deploy/iis/?view=aspnetcore-2.2 - user9542490
首先,尝试将 app.UseDeveloperExceptionPage(); 移出 if 语句 并与我们分享错误响应。你连接了本地数据库吗?尝试通过更改 IIS 应用程序池中的身份来在您的帐户下运行网站。 - Edward
2个回答

1

为了使其正常工作,您需要设置IIS。

enter image description here

你还需要确保使用 .NET Core Windows Server Hosting Bundle
然后:
执行以下命令之一:重启系统或 net stop was /y,接着是从命令行运行 net start w3svc。重新启动 IIS 会获取由安装程序更改的环境变量系统 PATH 的更改。
之后以管理员身份打开命令提示符并键入:
C:\Windows\System32> iisreset
然后将应用程序发布到文件夹并在那里打开命令提示符。通过输入以下命令来运行应用程序
C:\Temp\publish> dotnet YourApplicationName.dll
现在您可以转到浏览器并键入 http://localhost:port/,它将显示您的 .Net Core 应用程序。

不起作用。我认为问题是关于我的数据库连接。默认值API在不使用数据库的情况下工作得很好。 - A. Jafarzadeh

0
使用SQL Server身份验证对我来说很有效。 我使用的连接字符串如下:
Server=.;
Database=myDB;
User Id=sa2;
Password=myPass;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接