HTTP Error 500: localhost is currently unable to h

2019-06-18 03:55发布

I'm running into an HTPP Error 500 and I'm not sure why. When I start my service, I pop open a Chrome browser and navigate to http://localhost:5000, and the error pops up. The Chrome Developer Tools windows shows this single error:

Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://localhost:5000/

Here is my Startup.cs file (exluding using statements for simplicity):

namespace Tuner
{
    public class Startup
    {
        public static void Main(string[] args)
        {
            var exePath = Process.GetCurrentProcess().MainModule.FileName;
            var directoryPath = Path.GetDirectoryName(exePath);

                                var host = new WebHostBuilder()
               .CaptureStartupErrors(true)
               .UseKestrel()
               .UseUrls("http://localhost:5000")
               .UseContentRoot(Directory.GetCurrentDirectory())
               .UseIISIntegration()
               .UseStartup<Startup>()
               .Build();
            host.Run();
        }


        public Startup(IHostingEnvironment env)
        {
            //Setup Logger
            Log.Logger = new LoggerConfiguration()
                .WriteTo.Trace()
                .MinimumLevel.Debug()
                .CreateLogger();
            // Set up configuration sources.
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json");
            //.AddEnvironmentVariables();
            Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; set; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSwaggerGen();

            services.AddMvc().AddJsonOptions(options =>
            {
                options.SerializerSettings.ContractResolver =
                    new CamelCasePropertyNamesContractResolver();
            });
        }

        // 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, IApplicationLifetime lifetime)
        {

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}");
            });


            lifetime.ApplicationStopping.Register(() =>
            {
                Log.Debug("Application Stopping. Do stuff.");
            });
        }
    }
}

With MVC, this causes the HomeController Index method to get called:

namespace Tuner.Controllers
{
    public class HomeController : Controller
    {
        public string appVersion = typeof(HomeController).Assembly.GetName().Version.ToString();
        public string appName = "Empty Web App";

        [HttpGet("/")]
        public IActionResult Index()
        {
            var url = Request.Path.Value;
            if (url.EndsWith(".ico") || url.EndsWith(".map"))
            {
                return new StatusCodeResult(404);
            }
            else
            {
                // else block is reached
                return View("~/Views/Home/Index.cshtml");
            }
        }

        public IActionResult Error()
        {
            return View("~/Views/Shared/Error.cshtml");
        }

        [HttpGetAttribute("app/version")]
        public string Version()
        {
            return appVersion;

        }

        [HttpGetAttribute("app/name")]
        public string ProjectName()
        {
            return appName;
        }
    }
}

and here is my Index.cshtml file (which has been placed in Views/Home):

@{
    ViewBag.Title = "Tuner";
}

@section pageHead {
}

@section scripts {
    <script src="~/vendor.bundle.js"></script> 
    <script src="~/main.bundle.js"></script>

}

<cache vary-by="@Context.Request.Path">
    <app>Loading content...</app>
</cache>

4条回答
太酷不给撩
2楼-- · 2019-06-18 04:31

In your setup the UseIISIntegration "interferes" with UseUrls, as the UseUrls setting is for the Kestrel process and not the IISExpress/IIS.

If you want to change the IIS Port, have a look at Properties/launchSettings.json - there you can configure the applicationUrl IIS is using.

You could remove the UseIISIntegration for testing purposes and then you can connect to Port 5000, but you never should use Kestrel as Internet facing Server, it should always be run behind a Reverse Proxy like IIS or Nginx, etc.

See the Hosting Docs Page for more information.

查看更多
Root(大扎)
3楼-- · 2019-06-18 04:37

Run asp.net MVC core 1.1 project in vs2017 also get this error.
Solved by upgrade all NuGet Packages version 1.x to latest 2.x and target framework to .NET Core 2.1.

查看更多
狗以群分
4楼-- · 2019-06-18 04:38

You, like me, might need to install ASP.NET!

On Windows Server 2012 R2 this can be done via the Turn Windows features on or off feature:

  1. Complete the Before You Begin, Installation Type, and Server Selection steps of the wizard.
  2. Under Server Roles, find the Web Server (IIS) node and expand it.
  3. Expand the Web Server node.
  4. Expand the Application Development node.
  5. Check the ASP.NET 3.5 or ASP.NET 4.5 nodes, as appropriate.
  6. Finish the Add Roles and Features Wizard.
查看更多
戒情不戒烟
5楼-- · 2019-06-18 04:39

I was using IIS (I'm unclear if OP was trying to use IIS), but I did not Install the .NET Core Windows Server Hosting bundle, as described in instructions like this one. After installing that Module, my app served (i.e. no 500 error)

查看更多
登录 后发表回答