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

时间:2018-04-06 19:15:05

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

我有一个非常简单的.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版本,以及其他文章建议的其他一些小更改。

我仍然无法逃避。任何建议。

4 个答案:

答案 0 :(得分:0)

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

viber

答案 1 :(得分:0)

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

在.csproj文件中将PublishWithAspNetCoreTargetManifest设置为false是

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

我想您的路由尚未完成。请尝试使用 Url + / api / ballot

示例:https://xxxx.azurewebsites.net/api/ballot。 这里的 https://xxxx.azurewebsites.net 是您的天蓝色网址,而 api / ballot 是您在launchSettings.json文件(例如“ launchUrl”)中定义的启动网址:“ api / ballot /”,

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

应该可以。