7
votes

I'm struggling to get this WebAPI to work. Well, work with IIS. Everything works fine in IIS express, but when I publish it, specifically 1 api request doesn't work. I'm trying to access a url of API/[Controller]/{date}/{integer}. I keep getting the 500 server error. My other route of API/[Controller]/{date} works.

Here's my API Controller:

[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<>? I'm stumped considering it works fine in VS's IIS Express.

Edit

Here's my startup.cs page:

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));
            }
2
Did you check the server's event log? Usually a 500 is caused by an exception and often you can find those details in the Application Log on the IIS machine.CodingGorilla
you could investigate to the use of your string date parameteralessandro
@Infer-On What do you mean?Brandon
@CodingGorilla The logs don't have anything in particular. Just some Time Idle logs and some 404s. Is the syntax correct? e.g. can I pass in a string parameter, then an integer? ({date} being the string, and {modeID} being the int)Brandon
Now, I haven't done any WebAPI mapping in my startup.cs file. I've done 1 mapping for the application itself.Brandon

2 Answers

12
votes

That's a good thought that it might that fact that you're returning a List. We have working Core Web API methods and all of them return Task<IEnumerable<Foo>>. Try changing the return type List<TruckPosting> to 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:

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". 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.

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

I posted an ASP.NET Core custom exception handler on GitHub today. I thought it might be helpful to you since you're building an SPA and are probably calling a lot Web API methods.

It's a middleware project that returns error messages for Web API calls and redirects to an error page for non Web API calls. It can also log the exceptions in a SQL Server database. 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.

Here's the link.
https://github.com/ClintBailiff/CustomExceptionHandler

If you end up checking it out and have some suggestions or comments, I like to hear them.