简体   繁体   English

IIS 7.5中的ASP.NET Core WebAPI 500内部错误

[英]ASP.NET Core WebAPI 500 Internal error in IIS 7.5

I'm struggling to get this WebAPI to work. 我正在努力让这个WebAPI工作。 Well, work with IIS. 好吧,使用IIS。 Everything works fine in IIS express, but when I publish it, specifically 1 api request doesn't work. 在IIS express中一切正常,但是当我发布它时,特别是1 api请求不起作用。 I'm trying to access a url of API/[Controller]/{date}/{integer} . 我正在尝试访问API/[Controller]/{date}/{integer}的网址。 I keep getting the 500 server error. 我一直收到500服务器错误。 My other route of API/[Controller]/{date} works. 我的其他API/[Controller]/{date}路由有效。

Here's my API Controller: 这是我的API控制器:

[Route("api/[controller]")]
    public class PostingsController : Controller
    {
        // GET: api/Postings/5
        [HttpGet("{date}")]
        public string Get(string date)
        {
            return date;
        }

        // GET api/Postings/20160407/2
        [HttpGet("{date}/{modeID}")]
        public List<TruckPosting> Get(string date, int modeID)
        {
            TruckPosting tp = new TruckPosting();
            List<TruckPosting> truckPostings = tp.GetTruckPostings(date, modeID);
            return truckPostings;
        }
    }

Could the reason be that I'm trying to return a List<>? 原因可能是我正在尝试返回List <>? I'm stumped considering it works fine in VS's IIS Express. 考虑到它在VS的IIS Express中工作得很好,我很难过。

Edit 编辑

Here's my startup.cs page: 这是我的startup.cs页面:

public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }

public void Configure1(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
            {
                app.UseIISPlatformHandler();
                app.UseDefaultFiles();
                app.UseStaticFiles();
                app.UseFileServer(true);
                app.UseMvc();
            }

            // 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)
            {
                app.Map("/appRoot", (app1) => this.Configure1(app1, env, loggerFactory));
            }

That's a good thought that it might that fact that you're returning a List. 这是一个很好的想法,你可能会返回一个List。 We have working Core Web API methods and all of them return Task<IEnumerable<Foo>> . 我们有工作的Core Web API方法,所有这些方法都返回Task<IEnumerable<Foo>> Try changing the return type List<TruckPosting> to Task<IEnumerable<TruckPosting>> 尝试将返回类型List<TruckPosting>更改为Task<IEnumerable<TruckPosting>>

EDIT: To view the details for 500 (internal server) errors you will need to put the following line of code at the beginning of your Configure (or Configure1) method: 编辑:要查看500(内部服务器)错误的详细信息,您需要将以下代码行放在Configure(或Configure1)方法的开头:

app.UseDeveloperExceptionPage();   

And obviously this is not something you want in a production environment. 显然,这不是您在生产环境中想要的东西。

EDIT2: When running in VS you can use the code below to show exception details as long as the Hosting:Environment variable in the Debug section of Properties is set to "Development". 编辑2:在VS中运行时,只要“属性”的“调试”部分中的“主机:环境”变量设置为“开发”,就可以使用下面的代码显示异常详细信息。 After publishing you will need to create a System Environment variable named ASPNET_ENV and set its value to "Development" or the code will not call the UseDeveloperExceptionPage() method. 发布后,您需要创建一个名为ASPNET_ENV的系统环境变量,并将其值设置为“Development”,否则代码将不会调用UseDeveloperExceptionPage()方法。

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}

I posted an ASP.NET Core custom exception handler on GitHub today. 我今天在GitHub上发布了一个ASP.NET Core自定义异常处理程序。 I thought it might be helpful to you since you're building an SPA and are probably calling a lot Web API methods. 我认为它可能对您有所帮助,因为您正在构建SPA并且可能正在调用许多Web API方法。

It's a middleware project that returns error messages for Web API calls and redirects to an error page for non Web API calls. 这是一个中间件项目,它返回Web API调用的错误消息,并重定向到非Web API调用的错误页面。 It can also log the exceptions in a SQL Server database. 它还可以记录SQL Server数据库中的异常。 That's done in a separate thread to help with performance. 这是在一个单独的线程中完成的,以帮助提高性能。

The solution includes a demo application that shows how to use the exception handler for both Web API exceptions and non Web API exceptions. 该解决方案包括一个演示应用程序,该应用程序演示如何对Web API异常和非Web API异常使用异常处理程序。

Here's the link. 这是链接。
https://github.com/ClintBailiff/CustomExceptionHandler https://github.com/ClintBailiff/CustomExceptionHandler

If you end up checking it out and have some suggestions or comments, I like to hear them. 如果您最终查看并提出一些建议或意见,我希望听到他们的意见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM