首页 > 解决方案 > 将 ASP.NET Core Web API 发布到 IIS 的问题

问题描述

我在尝试将我的 asp.net 核心 Web API 项目部署到 IIS 时遇到问题。该项目在 localhost 上运行良好,但是当部署到 IIS 时,我在复杂的查询控制器上收到内部 500 错误,并且无法为脚手架编辑的控制器获得任何响应。但是,预建值控制器响应良好。

我正在从 mssql 数据库中提取数据。

我的控制器类错了吗?或者我的 sql 连接在 IIS 中不起作用?

有人可以帮我指出正确的方向吗?

这是我的控制器、启动和程序文件的屏幕截图。

我已经尝试了网络上可用的所有内容。

这是我的文件的屏幕截图。

我的控制器复杂查询 *****

public IEnumerable<SalesOrder> GetSalesOrder()
{
    var salesOrders = _context.SalesOrder.Include("LineItem").Select(s => new SalesOrder
    {
        // Sales orders
        id = s.id,
        orderId = s.orderId,
        creationDate = s.creationDate,
        lastModifiedDate = s.lastModifiedDate,
        legacyOrderId = s.legacyOrderId,
        orderFulfillmentStatus = s.orderFulfillmentStatus,
        orderPaymentStatus = s.orderPaymentStatus,
        sellerId = s.sellerId,
        username = s.username,
        priceSubtotal = s.priceSubtotal,
        paymentMethod = s.paymentMethod,
        paymentReferenceId = s.paymentReferenceId,
        shipToName = s.shipToName,
        addressLine = s.addressLine,
        city = s.city,
        stateOrProvince = s.stateOrProvince,
        postalCode = s.postalCode,
        countryCode = s.countryCode,
        phoneNumber = s.phoneNumber,
        email = s.email,
        shippingCarrierCode = s.shippingCarrierCode,
        estimatedDeliveryDate = s.estimatedDeliveryDate,
        dateAdded = s.dateAdded,
        // Line items
        LineItems = s.LineItems.Select(l => new LineItem
        {
            id = l.id,
            lineItemId = l.lineItemId,
            legacyItemId = l.legacyItemId,
            sku = l.sku,
            title = l.title,
            lineItemCost = l.lineItemCost,
            currency = l.currency,
            quantity = l.quantity,
            dateAdded = l.dateAdded,
            orderId = l.orderId
        })
    }).OrderByDescending(d => d.creationDate).ToList();

    return salesOrders;
}

启动文件*********

公共类启动{公共启动(IConfiguration配置){配置=配置;}

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.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    services.AddCors();

    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseCors(builder =>
    builder.WithOrigins(Configuration["ApplicationSettings:Client_URL"]).AllowAnyHeader().AllowAnyMethod());


    app.UseAPIKeyMessageHandlerMiddleware();

    app.UseMvc();
}

}

程序文件 *************

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}

标签: asp.netasp.net-coreiisasp.net-web-api

解决方案


500 错误可能很多。

或者,只需设置您自己的注册表项。(我只是记住了它们,因为我一直在设置它们。)将 HKLM\Software\Microsoft\Fusion\ForceLog 注册表值设置为 1,将 HKLM\Software\Microsoft\Fusion\LogPath 注册表值设置为 C:\FusionLogs 或某个路径存在。

打开 Fusion Logging 后,我可以立即在我的文件夹中看到故障。


推荐阅读