.NET Core - 仅在服务器上出现404错误

问题描述 投票:1回答:3

我有一个非常简单的.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();
        }
    }
}

Program.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/"
    }
  }
}

enter image description here

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

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

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版本,以及其他文章建议的其他一些小更改。

我仍然无法逃避。有什么建议。

c# .net-core iis-10 razor-pages
3个回答
0
投票

您的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>

0
投票

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

没有必要在.csproj文件中将PublishWithAspNetCoreTargetManifest设置为false。

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


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.