剃刀找不到类型或名称空间 [英] Razor The type or namespace could not be found

查看:54
本文介绍了剃刀找不到类型或名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与这一个非常相似.

嗨.
我有一个针对.NET 4.61的应用程序(服务),并尝试使用WebListener迁移到AspNetCore自托管服务器.
我正在尝试通过在控制台应用程序中进行测试来调查需求.
创建的项目是正常的Windows控制台应用程序,我使用nuget引用了所有必需的程序包,并创建了必需的Startup类,并使用WebHostBuilder创建和启动主机.

Hi.
I have an application (Service) targeting .NET 4.61 and trying to move to AspNetCore self host with WebListener.
I am trying to investigate the requirements by testing it out in a console application.
The project i crated is a normal Windows console application, i referenced all the required packages with nuget and created required Startup class and create and start the host with WebHostBuilder.

(我没有普通的Windows控制台应用程序,但是我的ubuntu笔记本电脑上有一个dotnet控制台应用程序)

(I don't have a normal Windows console application, but a dotnet console application on my ubuntu laptop)

我们在尝试之前已经进行了一些尝试.

We've differed a little bit in what we've tried and tested before hand.

到目前为止,我只创建了HomeController并让Index"Hello world"返回了string.运行该应用程序,转到localhost并得到响应.完美.

Up until now I just made a HomeController and had Index return a string with "Hello world". Ran the application, went to localhost and got the response. Perfect.

现在我所添加的只是Index通过返回View()返回IActionResult.然后我在/Views/Home/中制作了一个Index.cshtml文件,现在我明白了:

Now all I've added was that Index return an IActionResult by returning View(). Then I made an Index.cshtml file in /Views/Home/ and now I get this:

失败:Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware [0] 发生未处理的异常:发生一个或多个编译失败:

fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[0] An unhandled exception has occurred: One or more compilation failures occurred:

后接150行,如下所示:

followed by 150 lines like this:

gee1fd0w.wuj(4,11):错误CS0246:找不到类型或名称空间名称'System'(您是否缺少using指令或程序集引用?)
gee1fd0w.wuj(5,11):错误CS0246:找不到类型或名称空间名称系统"(您是否缺少using指令或程序集引用?)
gee1fd0w.wuj(6,11):错误CS0246:找不到类型或名称空间名称系统"(您是否缺少using指令或程序集引用?)
...
gee1fd0w.wuj(37,13):错误CS0518:未定义或导入了预定义类型'System.Object'
gee1fd0w.wuj(37,13):错误CS0518:未定义或导入了预定义类型'System.Void'

gee1fd0w.wuj(4,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
gee1fd0w.wuj(5,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
gee1fd0w.wuj(6,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
...
gee1fd0w.wuj(37,13): error CS0518: Predefined type 'System.Object' is not defined or imported
gee1fd0w.wuj(37,13): error CS0518: Predefined type 'System.Void' is not defined or imported

结尾为:

gee1fd0w.wuj(33,36):错误CS0161:"_ Views_Home_Index_cshtml.ExecuteAsync()":并非所有代码路径都返回值
在Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationResult.EnsureSuccessful()
在Microsoft.AspNetCore.Mvc.Razor.Internal.CompilerCache.CreateCacheEntry(String relativePath,String normalizedPath,Func'2编译)
---从之前引发异常的位置开始的堆栈跟踪---

gee1fd0w.wuj(33,36): error CS0161: '_Views_Home_Index_cshtml.ExecuteAsync()': not all code paths return a value
at Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationResult.EnsureSuccessful()
at Microsoft.AspNetCore.Mvc.Razor.Internal.CompilerCache.CreateCacheEntry(String relativePath, String normalizedPath, Func'2 compile)
--- End of stack trace from previous location where exception was thrown ---

此之后:

在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)上
在Microsoft.AspNetCore.Mvc.Razor.Internal.CompilerCache.GetOrAdd(String relativePath,Func'2 compile)
在Microsoft.AspNetCore.Mvc.Razor.Internal.DefaultRazorPageFactoryProvider.CreateFactory(String relativePath)
在Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.CreateCacheResult(HashSet`1 expirationTokens,字符串relativePath,布尔值isMainPage中)
在Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.OnCacheMiss(ViewLocationExpanderContext expanderContext,ViewLocationCacheKey cacheKey)
在Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.LocatePageFromViewLocations(ActionContext actionContext,String pageName,Boolean isMainPage)
在Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.FindView(ActionContext上下文,字符串viewName,布尔值isMainPage)
在Microsoft.AspNetCore.Mvc.ViewEngines.CompositeViewEngine.FindView(ActionContext上下文,字符串viewName,布尔值isMainPage)
在Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.ViewResultExecutor.FindView(ActionContext actionContext,ViewResult viewResult)
在Microsoft.AspNetCore.Mvc.ViewResult.d__26.MoveNext()
---从之前引发异常的位置开始的堆栈跟踪---

at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Razor.Internal.CompilerCache.GetOrAdd(String relativePath, Func'2 compile)
at Microsoft.AspNetCore.Mvc.Razor.Internal.DefaultRazorPageFactoryProvider.CreateFactory(String relativePath)
at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.CreateCacheResult(HashSet`1 expirationTokens, String relativePath, Boolean isMainPage)
at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.OnCacheMiss(ViewLocationExpanderContext expanderContext, ViewLocationCacheKey cacheKey)
at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.LocatePageFromViewLocations(ActionContext actionContext, String pageName, Boolean isMainPage)
at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.FindView(ActionContext context, String viewName, Boolean isMainPage)
at Microsoft.AspNetCore.Mvc.ViewEngines.CompositeViewEngine.FindView(ActionContext context, String viewName, Boolean isMainPage)
at Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.ViewResultExecutor.FindView(ActionContext actionContext, ViewResult viewResult)
at Microsoft.AspNetCore.Mvc.ViewResult.d__26.MoveNext()
--- End of stack trace from previous location where exception was thrown ---

随后是一系列较短的堆栈跟踪:

Followed by a series of shorter stack traces:

在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)上
在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__30.MoveNext()
---从之前引发异常的位置开始的堆栈跟踪---
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()处
在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)上
在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__28.MoveNext()
---从之前引发异常的位置开始的堆栈跟踪---
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()处
在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ResultExecutedContext上下文)
在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& Next,Scope&范围,Object& State,Boolean& isCompleted)
在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__22.MoveNext()
---从上一个引发异常的位置开始的堆栈结束跟踪--- 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()处
在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ResourceExecutedContext上下文)
在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& Next,Scope&范围,Object& State,Boolean& isCompleted)
在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__20.MoveNext()
---从之前引发异常的位置开始的堆栈跟踪---
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()处
在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)上
在Microsoft.AspNetCore.Builder.RouterMiddleware.d__4.MoveNext() ---从之前引发异常的位置开始的堆栈跟踪---
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()处
在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)上
在Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.d__6.MoveNext()

at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__30.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__28.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ResultExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ResourceExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__20.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Builder.RouterMiddleware.d__4.MoveNext() --- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.d__6.MoveNext()

堆栈跟踪结束

现在我不确定我是否完全理解上一张海报的答案:

End of stack trace

Now I'm not sure that I fully understand the answer the previous poster got:

Mvc的运行时编译依赖DependencyContext(deps文件)来查找编译引用,该引用仅在使用dotnet CLI进行编译时产生.您必须在应用程序启动过程中添加编译引用,才能使视图编译正常工作.

Mvc's runtime compilation relies on DependencyContext (deps file) to look up compilation references which is only produced when you compile using dotnet CLI. You would have to add compilation references as part of the application startup for view compilation to work.

我确实通过dotnet cli进行了编译,但是这里还有其他我想念的东西吗?当我尝试通过运行dotnet new mvc并删除所有内容(控制台控制台项目中的内容除外)来复制项目时.恰好在下雨,我从Index.cshtml获得了问候.

I did compile through dotnet cli, but is there something else that I'm missing here? When I try to replicate my project by running dotnet new mvc and deleting everything but the things that are in my console-project. It runs right as rain and I get hello world from my Index.cshtml.

本能告诉我问题出在某些文件差异上,但是我已经对所有文件进行了差异化,除了app-name之外我什么都找不到.我本可以错过一些东西,但我想知道这是否可能是我不知道的自动事物?

Instinct tells me that the problem lies in some file-difference, but I've diffed all of the files and I can't find any difference but app-name. I could have missed something but I'm wondering if it could perhaps be something automagical that I'm not aware of?

已经发现了问题,看起来好像是它们生成的bin/debug/netcoreapp1.1/<appname>.deps.json有所不同.现在要研究一些差异.但是,如果我将文件从mvc-project复制粘贴到控制台应用程序,则一切运行正常.

Have found the issue, looks like it's a difference in their generated bin/debug/netcoreapp1.1/<appname>.deps.json. Quite a few differences, going to look into it now. But if I copy paste that file from the mvc-project to the console app, everything runs ok.

不幸的是,控制台应用程序deps.json-文件的长度为1300+行,而mvc应用程序的版本为4300+行,所以我不确定要遍历它并确定为什么一个可行而另一个可行不要.

Unfortunately the console applications deps.json-file is 1300+ lines long and the mvc applications version is 4300+ lines long so I'm not entirely sure it will be feasible to go through it and identifying why one works and the other don't.

似乎大多数依赖范围是不同的,很多mvc包含对"NETStandard.Library": "1.6.1"的依赖,但也有很多.还有一堆控制台应用程序所没有的编译范围.

It mostly seems to be a whole lot of dependency scopes that differ, a whole lot of the mvc ones contain dependencies to "NETStandard.Library": "1.6.1", but many others as well. There's also a bunch of compile-scopes that the console-application don't have.

实际上,这似乎是唯一的区别. mvc应用程序的deps.json文件似乎具有〜3000多行依赖项和编译范围.所以我想我会放弃找出导致问题的原因的想法,并且放弃学习如何将控制台应用程序构建为mvc-app"的计划:)

In fact this seems to be the only difference. The mvc-application's deps.json-file seem to have ~3000 more lines of dependencies and compile-scopes. So I think I'll give up the idea of figuring out what's causing the problem and and my plan to learn how to "build a console app into an mvc-app" :)

如果有人遇到类似情况,我将在此保留问题.

I'll leave the question here in case anyone find themselves in a similar situation.

推荐答案

我以前曾经发生过这种情况.

I have had this happen to me before.

执行dotnet restore并删除objbin目录.

在技术上我可能不是正确的,但是我认为这里发生的是您的旧版本与具有新(或不同)引用的新版本产生冲突.

I may not be technically correct but I think what's happening here is your old build is creating conflicts with your new build that has new (or different) references.

这篇关于剃刀找不到类型或名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆