【问题标题】:.NET Core - 404 error on server only.NET Core - 仅在服务器上出现 404 错误
【发布时间】:2018-09-16 21:16:07
【问题描述】:

我有一个非常简单的 .NET Core 测试应用程序,它在我的开发机器上运行,但在 IIS 10 下在我的服务器上运行时会导致 404。最初,目标是返回一些 JSON 格式的数据库记录,例如 Web 服务.但是,为了缩小错误的搜索范围,我已将应用程序更改为返回单个常量字符串。结果是一样的——在开发机器上工作,在服务器上失败。

在开发机器上,我从 Visual Studio 15.5.7 运行它。我使用 Web Deploy 发布到服务器。

根据其他帖子,我也尝试为 IIS 应用程序池设置“无托管代码”,但没有任何区别。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace api.iVoterGuide.com.Controllers
{
    [Route("api/ballot")]
    public class BallotController: Controller {
        // GET api/value  --  ballot/542
        [HttpGet("{eleck}")]
        public IEnumerable<string> Get(short elecK)
        {
            yield return "[ 1, 2, 3]";
        }
    }
}

这里是 Startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace api.iVoterGuide.com
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

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

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseMvc();
        }
    }
}

程序.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace api.iVoterGuide.com
{
    public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build();
    }
}

web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
     <!-- It works either with or without this CORS code -->
     <system.webServer>
          <handlers accessPolicy="Read, Execute, Script" />
          <httpProtocol>
               <customHeaders>
                    <add name="Access-Control-Allow-Origin" value="*" />
               </customHeaders>
          </httpProtocol>
      </system.webServer>
</configuration>

Release.pubxml(已编辑)

<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit https://go.microsoft.com/fwlink/?LinkID=208121. 
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <PublishProvider>FileSystem</PublishProvider>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <ProjectGuid>e86ba648-3c13-472c-b91c-1d0925762870</ProjectGuid>
    <publishUrl>bin\Release\PublishOutput</publishUrl>
    <DeleteExistingFiles>True</DeleteExistingFiles>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>
</Project>

launchSettings.json

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:57342/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "api.iVoterGuide.com": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://localhost:57343/"
    }
  }
}

有人有什么建议吗?提前致谢。

编辑:按照另一篇文章(我再也找不到)的建议,我尝试从命令行运行我的应用程序。

D:\wwwroot\api.iVoterGuide.com>dotnet .\api.ivoterguide.com.dll
Error: An assembly specified in the application dependencies manifest (api.ivoterguide.com.deps.json) was not found:
package: 'Microsoft.ApplicationInsights.AspNetCore', version: '2.1.1'
path: 'lib/netstandard1.6/Microsoft.ApplicationInsights.AspNetCore.dll'
This assembly was expected to be in the local runtime store as the application was published using the following target manifest files:
aspnetcore-store-2.0.0-linux-x64.xml;aspnetcore-store-2.0.0-osx-x64.xml;aspnetcore-store-2.0.0-win7-x64.xml;aspnetcore-store-2.0.0-win7-x86.xml

这是服务器上的 --info 结果:

D:\wwwroot\api.iVoterGuide.com>dotnet --info
Microsoft .NET Core Shared Framework Host

Version  : 2.0.6
Build    : 74b1c703813c8910df5b96f304b0f2b78cdf194d

即使有了这些信息,我也无法解决问题。我已经尝试安装最新的 .NET 版本,在我的 csproj() 中更改 .NET Core 版本,以及其他文章建议的其他几个小改动。

我仍然无法跑步。任何建议。

【问题讨论】:

  • Microsoft 在此处发布了详细的分步说明:docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/…您是否遵循了所有这些步骤?
  • 感谢两位的帮助。我阅读了有关 dotnet publish 的信息,并且由于它是经过批准的方式,因此我更改为使用它。我会将上面的 pubxml 编辑为我的新文件。不幸的是,我仍然得到 404。如果您有其他想法,我将不胜感激。
  • 从命令行运行显示了它缺少程序集的附加信息。我已将详细信息添加到主要帖子的末尾。我仍然无法让它运行。

标签: c# .net-core iis-10 razor-pages


【解决方案1】:

您的 web.config 文件应包含以下内容:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule"
resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\{dll's} path.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
</system.webServer>
</configuration>

【讨论】:

  • 谢谢。我通过 web.config 更改为看起来像你的,但我仍然得到 404。
  • 您能否发送屏幕截图或更详细的错误信息?
  • 您还可以通过在已发布的文件夹中添加日志文件夹并更改 web.config stdoutLogEnabled="true" 来获取日志。这将在您的日志文件夹中生成文件,您可以在其中获得更详细的错误描述甚至堆栈跟踪。
  • 还有在创建IIS网站的时候,你指定了哪个物理路径?
  • 我已经添加了从命令行运行它时的确切消息,表明它是一个缺失的程序集。这和截图一样好吗?物理路径为 D:\wwwroot\api.iVoterGuide.com。
【解决方案2】:

解决方案是在服务器as described here 上安装 .Net Core Windows Server Hosting。可以是downloaded here

不需要在 .csproj 文件中将 PublishWithAspNetCoreTargetManifest 设置为 false。

我可以从 Visual Studio 或使用命令行(dotnet publish -c Release -o bin\PublishOutput)发布。后者要快得多。

【讨论】:

    【解决方案3】:

    检查 IIS 站点设置中的绑定。一个可能的原因是,如果绑定中指定了 IP 地址并且它被另一个应用程序使用

    【讨论】:

      【解决方案4】:

      我猜你的路由没有完成。试试你的 Url+/api/ballot

      例如:https://xxxx.azurewebsites.net/api/ballot。 这里 https://xxxx.azurewebsites.net 是您的 azure url,api/ballot 是您在 launchSettings.json 文件中定义的启动 url,例如 "launchUrl": "api/ballot/",

      [Route("api/[controller]")] 应该在您的控制器类中。

      它应该可以工作。

      【讨论】:

        猜你喜欢
        • 2019-03-20
        • 2016-11-13
        • 2016-10-19
        • 1970-01-01
        • 2019-10-24
        • 1970-01-01
        • 2018-01-28
        • 2018-08-02
        • 2015-06-01
        相关资源
        最近更新 更多