IIS 10上的ASP.NET Core 404错误 [英] ASP.NET Core 404 Error on IIS 10

查看:1849
本文介绍了IIS 10上的ASP.NET Core 404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在IIS 10上运行的ASP.NET Core Web应用程序有问题. 我正在使用AngularJS开发单页应用程序.

I have a problem with ASP.NET Core web application running on IIS 10. I am developing a Single Page Application with AngularJS.

index.html可以完美加载,但是后端请求在IIS 10上失败并显示404错误代码.从带有IIS Express的Visual Studio,它可以完美运行.

The index.html loads perfectly but the backend requests are failing with 404 error code on the IIS 10. From Visual Studio with IIS Express it works perfectly.

任何人都可以发现我如何解决后端请求吗?

Can anyone spot how can I fix the backend requests?

这是我的Program.cs

Here's my Program.cs

public static void Main(string[] args)
{
    var host = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();

    host.Run();
}

这是我来自Startup.cs的Configure方法

And here's my Configure method from Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseDefaultFiles();

    app.UseStaticFiles();

    app.UseIdentity();

    // Custom middleware for Angular UI-Router
    app.Use(async (context, next) =>
    {
        if (!Path.HasExtension(context.Request.Path.Value)
        && context.Request.HttpContext.Request.Headers["X-Requested-With"] != "XMLHttpRequest"
        && context.Request.Method.ToUpper() != "POST"
        && context.Request.Method.ToUpper() != "PUT"
        && context.Request.Method.ToUpper() != "DELETE")
        {
            await context.Response.WriteAsync(File.ReadAllText(env.WebRootPath + "/index.html"));
        }

        await next();
    });

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

推荐答案

您的代码正在使用Kestrel在我的计算机上工作.一个很好的疑难解答步骤是找出问题是否出在您的ASP.NET Core应用程序或IIS主机配置上.

You code is working on my machine with Kestrel. A good troubleshooting step is to find out whether the problem is with your ASP.NET Core application or with your IIS Hosting configuration.

从您的项目的根开始尝试.

Try this from the root of your project.

dotnet restore
dotnet run

您将看到类似这样的内容:

You will see something like this:

Hosting environment: Production
Content root path: C:\MyApplicationPath
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

在浏览器中,转到以下两个URL.如果它们不起作用,则说明您的应用程序有问题.如果它们确实起作用,则说明您的IIS托管出现了问题.

In your browser go to the following two URLs. If they don't work, then something is wrong with your application. If they do work, something is wrong with your IIS hosting.

localhost:5000        // you will see your index.html page
localhost:5000/api    // you will see your default routes output

这篇关于IIS 10上的ASP.NET Core 404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆