gpt4 book ai didi

iis - 在 IIS 上部署 ASP.Net core 1 应用程序(404 错误)

转载 作者:行者123 更新时间:2023-12-02 03:17:50 24 4
gpt4 key购买 nike

我正在努力让我的 ASP.Net 核心 1 (asp.net 5/MVC 6) 应用程序在我的网络服务器上的 IIS 中运行。我已经按照指南在服务器上完成了以下操作

  • 通过 get.asp.net 安装 ASP.Net 5
  • 安装 HttpPlatformHandler 1.2

我已经检查过我可以在服务器上运行 dnx,并且编译的位是 64 位的,应用程序池是“无托管代码”并且以 64 位运行。

我可以通过运行 web.cmd 并导航到 http://localhost:5000 在网络服务器上运行该应用程序(或任何端口),但是当我尝试将应用程序设置为默认网站中的应用程序并浏览到它(例如 http://localhost/MyMVC6App )时,我收到 404 错误。我检查过物理路径是否指向/MyMVC6App/wwwroot。我还检查了网络服务器/处理程序部分是否已解锁。

我还创建了一个普通的 ASP.Net 5/Core 1 应用程序,并在 2 个不同的服务器上得到了相同的 404 错误!

这是我的配置方法:

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();

app.UseApplicationInsightsRequestTelemetry();

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

app.UseIISPlatformHandler();

app.UseApplicationInsightsExceptionTelemetry();

app.UseStaticFiles();

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

有什么想法吗?

最佳答案

如果您的静态文件托管在 ASPNET Core 应用之外,解决此问题的最简单方法是将 IIS 应用程序名称添加为您在 MVC 中的路由的基础。

另一种方法是添加一个简单的中间件类来去除 IIS 应用程序名称,并使请求看起来对它下面的任何模块都是透明的。我还没有测试下面的代码但是:

using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;

namespace MyMVC6App
{
// You may need to install the Microsoft.AspNet.Http.Abstractions package into your project
public class RemoveIISAppNameMiddleware
{
private readonly RequestDelegate _next;

public RemoveIISAppNameMiddleware(RequestDelegate next)
{
_next = next;
}

public Task Invoke(HttpContext httpContext)
{
var newRequestPath = new PathString();
var requestPathToIgnore = new PathString("/MyMVC6App");

if (httpContext.Request.Path.StartsWithSegments(requestPathToIgnore))
{
httpContext.Request.Path.StartsWithSegments(requestPathToIgnore, out newRequestPath);
httpContext.Request.Path = newRequestPath;
}
return _next(httpContext);
}
}

// Extension method used to add the middleware to the HTTP request pipeline.
public static class RemoveIISAppNameMiddlewareExtensions
{
public static IApplicationBuilder UseRemoveIISAppName(this IApplicationBuilder builder)
{
return builder.UseMiddleware<RemoveIISAppNameMiddleware>();
}
}
}

然后在 app.UseIISPlatformHandler() 之后的配置方法中调用 app.UseRemoveIISAppName()。在这种情况下,您不需要在 MVC 路由中有任何其他路径。

关于iis - 在 IIS 上部署 ASP.Net core 1 应用程序(404 错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35550567/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com