我在 Ubuntu 上使用 .Net core 1.0 运行简单的“hello world”应用程序时遇到内部服务器错误。我确信我错过了一些简单的东西,但无法弄清楚它是什么。它一直在工作,直到我更换return Content("Hello, world!");
with return View();
在 HomeController 中,并添加using System.IO;
and .UseContentRoot(Directory.GetCurrentDirectory())
到 Program.cs。
我尝试过添加System
and System.X
我在 Startup.cs 中的依赖项列表无济于事。
程序.cs
using Microsoft.AspNetCore.Hosting;
using System.IO;
namespace aspnetcoreapp
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build();
host.Run();
}
}
}
项目.json
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.AspNetCore.StaticFiles": "1.0.0",
"Microsoft.NETCore.App": { "type": "platform", "version": "1.0.0" }
},
"frameworks": {
"netcoreapp1.0": {}
}
}
启动.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
namespace aspnetcoreapp
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app)
{
app.UseDeveloperExceptionPage();
app.UseStaticFiles();
app.UseMvc();
}
}
}
HomeController.cs
using Microsoft.AspNetCore.Mvc;
namespace aspnetcoreapp
{
[Route("/")]
[Route("[controller]")]
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
}
}
点网运行
Project aspnetcoreapp (.NETCoreApp,Version=v1.0) will be compiled because inputs were modified
Compiling aspnetcoreapp for .NETCoreApp,Version=v1.0
Compilation succeeded.
0 Warning(s)
0 Error(s)
Time elapsed 00:00:01.0784778
Hosting environment: Production
Content root path: /home/cvandal/code/aspnetcoreapp
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
Result
编译处理此请求所需的资源期间发生错误。请检查以下具体错误
详细信息并适当修改您的源代码。
生成的代码
The type or namespace name 'System' could not be found (are you
missing a using directive or an assembly reference?)
using System; The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
using System.Linq; The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
using System.Collections.Generic; The type or namespace name 'ViewFeatures' does not exist in the namespace
'Microsoft.AspNetCore.Mvc' (are you missing an assembly reference?)
using Microsoft.AspNetCore.Mvc.ViewFeatures; The type or namespace name 'System' could not be found (are you missing a using directive or
an assembly reference?)
using System.Threading.Tasks; Cannot define a class or member that utilizes 'dynamic' because the compiler required type
'System.Runtime.CompilerServices.DynamicAttribute' cannot be found.
Are you missing a reference?
public class _Views_Home_Index_cshtml : Microsoft.AspNetCore.Mvc.Razor.RazorPage Predefined type
'System.Boolean' is not defined or imported
public class _Views_Home_Index_cshtml : Microsoft.AspNetCore.Mvc.Razor.RazorPage The type 'Object' is
defined in an assembly that is not referenced. You must add a
reference to assembly 'System.Runtime, Version=4.1.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
public class _Views_Home_Index_cshtml : Microsoft.AspNetCore.Mvc.Razor.RazorPage The type 'Object' is
defined in an assembly that is not referenced. You must add a
reference to assembly 'System.Runtime, Version=4.1.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
public Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider
ModelExpressionProvider { get; private set; } Predefined type
'System.Object' is not defined or imported
public Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider
ModelExpressionProvider { get; private set; } The type or namespace
name 'ViewFeatures' does not exist in the namespace
'Microsoft.AspNetCore.Mvc' (are you missing an assembly reference?)
public Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider
ModelExpressionProvider { get; private set; } Predefined type
'System.Void' is not defined or imported
public Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider
ModelExpressionProvider { get; private set; } The type 'Object' is
defined in an assembly that is not referenced. You must add a
reference to assembly 'System.Runtime, Version=4.1.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
public Microsoft.AspNetCore.Mvc.IUrlHelper Url { get; private set; } Predefined type 'System.Object' is not defined or imported
public Microsoft.AspNetCore.Mvc.IUrlHelper Url { get; private set; } The type or namespace name 'IUrlHelper' does not exist in the
namespace 'Microsoft.AspNetCore.Mvc' (are you missing an assembly
reference?)
public Microsoft.AspNetCore.Mvc.IUrlHelper Url { get; private set; } Predefined type 'System.Void' is not defined or imported
public Microsoft.AspNetCore.Mvc.IUrlHelper Url { get; private set; } The type 'Object' is defined in an assembly that is not
referenced. You must add a reference to assembly 'System.Runtime,
Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
public Microsoft.AspNetCore.Mvc.IViewComponentHelper Component { get; private set; } Predefined type 'System.Object' is not defined
or imported
public Microsoft.AspNetCore.Mvc.IViewComponentHelper Component { get; private set; } The type or namespace name
'IViewComponentHelper' does not exist in the namespace
'Microsoft.AspNetCore.Mvc' (are you missing an assembly reference?)
public Microsoft.AspNetCore.Mvc.IViewComponentHelper Component { get; private set; } Predefined type 'System.Void' is not defined or
imported
public Microsoft.AspNetCore.Mvc.IViewComponentHelper Component { get; private set; } The type 'Object' is defined in an assembly that
is not referenced. You must add a reference to assembly
'System.Runtime, Version=4.1.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a'.
public Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json { get; private set; } Predefined type 'System.Object' is not defined or
imported
public Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json { get; private set; } The type or namespace name 'IJsonHelper' does not
exist in the namespace 'Microsoft.AspNetCore.Mvc.Rendering' (are you
missing an assembly reference?)
public Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json { get; private set; } Predefined type 'System.Void' is not defined or
imported
public Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json { get; private set; } The type 'Object' is defined in an assembly that
is not referenced. You must add a reference to assembly
'System.Runtime, Version=4.1.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a'.
public Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper Html { get; private set; } Predefined type 'System.Object' is not
defined or imported
public Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper Html { get; private set; } The type or namespace name 'IHtmlHelper<>'
does not exist in the namespace 'Microsoft.AspNetCore.Mvc.Rendering'
(are you missing an assembly reference?)
public Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper Html { get; private set; } The type 'Object' is defined in an assembly
that is not referenced. You must add a reference to assembly
'System.Runtime, Version=4.1.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a'.
public Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper Html { get; private set; } Predefined type 'System.Object' is not
defined or imported
public Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper Html { get; private set; } Cannot define a class or member that
utilizes 'dynamic' because the compiler required type
'System.Runtime.CompilerServices.DynamicAttribute' cannot be found.
Are you missing a reference?
public Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper Html { get; private set; } Predefined type 'System.Boolean' is not
defined or imported
public Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper Html { get; private set; } Predefined type 'System.Void' is not
defined or imported
public Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper Html { get; private set; } The return type of an async method must be
void, Task or Task
public override async Task ExecuteAsync() The type 'Object' is defined in an assembly that is not referenced. You must add a
reference to assembly 'System.Runtime, Version=4.1.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
public override async Task ExecuteAsync() Predefined type 'System.Object' is not defined or imported
public override async Task ExecuteAsync() The type or namespace name 'Task' could not be found (are you missing a using
directive or an assembly reference?)
public override async Task ExecuteAsync() '_Views_Home_Index_cshtml.ExecuteAsync()': return type must be 'Task'
to match overridden member 'RazorPage.ExecuteAsync()'
public override async Task ExecuteAsync() The type 'Task' is defined in an assembly that is not referenced. You must add a
reference to assembly 'System.Threading.Tasks, Version=4.0.10.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
public override async Task ExecuteAsync() Predefined type 'System.Void' is not defined or imported
public _Views_Home_Index_cshtml()
{
} The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime,
Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
[Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] Predefined type 'System.Object' is not defined or imported
[Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] The type 'Attribute' is defined in an assembly that is not referenced.
You must add a reference to assembly 'System.Runtime, Version=4.1.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
[Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] Predefined type 'System.Void' is not defined or imported
[Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] The type 'Object' is defined in an assembly that is not referenced.
You must add a reference to assembly 'System.Runtime, Version=4.1.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
[Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] Predefined type 'System.Object' is not defined or imported
[Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] The type 'Attribute' is defined in an assembly that is not referenced.
You must add a reference to assembly 'System.Runtime, Version=4.1.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
[Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] Predefined type 'System.Void' is not defined or imported
[Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] The type 'Object' is defined in an assembly that is not referenced.
You must add a reference to assembly 'System.Runtime, Version=4.1.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
[Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] Predefined type 'System.Object' is not defined or imported
[Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] The type 'Attribute' is defined in an assembly that is not referenced.
You must add a reference to assembly 'System.Runtime, Version=4.1.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
[Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] Predefined type 'System.Void' is not defined or imported
[Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] The type 'Object' is defined in an assembly that is not referenced.
You must add a reference to assembly 'System.Runtime, Version=4.1.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
[Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] Predefined type 'System.Object' is not defined or imported
[Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] The type 'Attribute' is defined in an assembly that is not referenced.
You must add a reference to assembly 'System.Runtime, Version=4.1.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
[Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] Predefined type 'System.Void' is not defined or imported
[Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] The type 'Object' is defined in an assembly that is not referenced.
You must add a reference to assembly 'System.Runtime, Version=4.1.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
[Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] Predefined type 'System.Object' is not defined or imported
[Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] The type 'Attribute' is defined in an assembly that is not referenced.
You must add a reference to assembly 'System.Runtime, Version=4.1.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
[Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] Predefined type 'System.Void' is not defined or imported
[Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute] Predefined type 'System.Void' is not defined or imported
public _Views_Home_Index_cshtml() Predefined type 'System.Int32' is not defined or imported
BeginContext(0, 181, true); Predefined type 'System.Int32' is not defined or imported
BeginContext(0, 181, true); Predefined type 'System.Boolean' is not defined or imported
BeginContext(0, 181, true); The type 'Object' is defined in an assembly that is not referenced. You must add a reference to
assembly 'System.Runtime, Version=4.1.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a'.
BeginContext(0, 181, true); Predefined type 'System.Object' is not defined or imported
BeginContext(0, 181, true); Predefined type 'System.Void' is not defined or imported
BeginContext(0, 181, true); Predefined type 'System.String' is not defined or imported
WriteLiteral("\n\n\n \n \n\n aspnetcoreapp\n
\n\n\n
你好世界!
\n
\n\n"); The type 'Object' is defined in an assembly that
is not referenced. You must add a reference to assembly
'System.Runtime, Version=4.1.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a'.
WriteLiteral("\n\n\n \n \n\n aspnetcoreapp\n
\n \n \n 你好世界!
\n
\n\n"); Predefined type 'System.Object' is not defined
or imported
WriteLiteral("\n\n\n \n \n\n aspnetcoreapp\n
\n \n \n 你好世界!
\n
\n\n"); Predefined type 'System.Void' is not defined or
imported
WriteLiteral("\n\n\n \n \n\n aspnetcoreapp\n
\n \n \n 你好世界!
\n
\n\n"); The type 'Object' is defined in an assembly that
is not referenced. You must add a reference to assembly
'System.Runtime, Version=4.1.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a'.
EndContext(); Predefined type 'System.Object' is not defined or imported
EndContext(); Predefined type 'System.Void' is not defined or imported
EndContext(); '_Views_Home_Index_cshtml.ExecuteAsync()': not all code paths return a value
public override async Task ExecuteAsync()