类型或命名空间'的mvc的名字并不在命名空间存在'System.Web程序“ [英] The type or namespace 'Mvc' name does not exist in the namespace 'System.Web'

查看:304
本文介绍了类型或命名空间'的mvc的名字并不在命名空间存在'System.Web程序“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用RazorEngine生成的电子邮件类库。 RazorEngine使用了MVC。
我在图书馆类引用System.Web.Mvc,将其设置为本地复制,这是在Bin文件夹present。不过,我仍然得到此异常。

I have a class library that uses RazorEngine to generate emails. RazorEngine uses Mvc. I have referenced System.Web.Mvc in my library class, set it to copy local, and it is present in the Bin folder. However I still get this exception.

我得到它的工作的唯一办法,就是实际调用从MVC空间像 MvcHtmlString测试=新MvcHtmlString(测试); 调用RazorEngine之前(解决方案找到<一href=\"http://stackoverflow.com/questions/17105370/cannot-use-mvchtmlstring-ihtmlstring-with-razorengine\">here)

The only way I got it working, is to actually call something from the Mvc namespace like MvcHtmlString test = new MvcHtmlString("test"); before calling RazorEngine (solution found here)

我想这不是世界末日,但我还是想解决此问题(或至少明白是什么导致了它)。干杯

I guess it's not the end of the world, but I would still like to resolve this problem (or at least understand what's causing it). Cheers

推荐答案

好了,我可以归结问题为以下内容:
你必须像一个自托管的WebAPI /控制台应用程序,并希望使用剃须刀
创建HTML页面。这些剃刀模板使用一些类从Sytem.Web.WebPages
或System.Web.Mvc或类似物,这是在自己的组件捆绑

OK, so I was able to boil the problem down to the following: You have something like a self-hosted WebAPI/console application, and want to use Razor to create HTML pages. Those Razor templates use some classes from Sytem.Web.WebPages or System.Web.Mvc or the like, which are bundled in their own assemblies.

但这些类在视图中使用的。当在调试模式下启动,你会看到
运行时没有的不可以加载这些组件,无论它们被添加到项目
引用或没有。编译剃刀文件时,它的出现,编译器只能看到命名空间
从加载的程序集,所以你得到这些的X未找到异常。你将不得不加载所需
手动组件(并确保兼容的版本被发现)。

But those classes are used only in the views. When starting in debug mode you will see that the runtime does not load those assemblies, regardless if they are added to the project references or not. It appears when compiling razor files, the compiler sees only namespaces from loaded assemblies, so you get those X-not-found exceptions. You will have to load the needed assemblies manually (and make sure a compatible version is found).

我们有一个layout.cshtml和index.cshtml。我们使用了一些HTML辅助函数来创建表单。
layout.cshtml:

We have a layout.cshtml and an index.cshtml. We use some HTML helper functions to create a form. layout.cshtml:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
</head>
<body>
  <div class="main-content">
    @RenderBody()
  </div>
</body>
</html>

index.cshtml:

index.cshtml:

@using System.Web.Mvc;
@{
  Layout = "layout";
}
<div class="box-body">
  <h1>Hello!</h1>
  <p>
    Got Html? @MvcHtmlString.Create("<b>FooBarBaz</b>")
  </p>
</div>

来源$ C ​​$ c其中剃刀文件进行分析和HTML输出处理。为简单起见它
假设所有的文件都是可执行附近;你可能需要去适应你的存储结构:

Source code where the razor files are parsed and HTML output is handled. For simplicity it is assumed all files are near the executable; you may need to adapt to your storage structure:

//load required assembly. By using the short name here the runtime uses
//the the first one that matches. See MSDN for further information
System.Reflection.Assembly.Load("System.Web.Mvc");
//cache the template before the actual page to avoid a null reference exception
RazorEngine.Razor.GetTemplate(File.ReadAllText("layout.cshtml"), "layout");
var result = RazorEngine.Razor.Parse(File.ReadAllText("index.cshtml"));
Console.Write(result);

然后,您需要(通过的NuGet在这个例子中可能Microsoft.AspNet.Mvc)到装配添加到项目中,也许手动添加引用(如果没有自动添加)。这样,你得到你的可执行文件一起复制的(希望)正确的版本。

You then need to add the assembly to your project (in this example possible via nuget, Microsoft.AspNet.Mvc) and maybe manually add the reference (if it isn't added automatically). This way you get the (hopefully) right version copied along with your executable.

在一个ASP.Net MVC视图中提供几乎所有的东西(@Html例如)是由物体注入
直接使用剃刀时,ASP.NET管道并为此不可用。 (我不知道是否或如何
可以通过模型之外的对象剃刀),所以大部分的类和函数
从System.Web.Mvc或System.Web.WebPages命名空间可能是遥不可及
(没有重建的ASP.NET份)。

Nearly all goodies available in an ASP.Net MVC view (@Html for example) are objects injected by the ASP.NET pipeline and therefor not available when using Razor directly. (I don't know if or how it is possible to pass objects to Razor besides models) So most classes and functions from the System.Web.Mvc or System.Web.WebPages namespaces are probably out of reach (without reconstructing parts of ASP.NET).

所使用的@Layout和@RenderBody()通常是其中的一些ASP.NET佣工了。但是,当他们是pretty常见,看来笔者在RazorEngine分析器手动重新创建它们的功能。

The used @Layout and @RenderBody() are usually some of those ASP.NET helpers too. But as they are pretty common, it appears the author recreated their function manually in the RazorEngine parser.

这篇关于类型或命名空间'的mvc的名字并不在命名空间存在'System.Web程序“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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