简体   繁体   English

Views / Web.config或@using都没有将MVC项目的命名空间添加到视图中

[英]Neither Views/Web.config or @using add MVC project's namespace to the view

I'm at wits' end here. 我在这里结束了。

Steps to reproduce at the bottom 在底部重现的步骤

I've created an MVC 4 project, say MyProj.Mvc , in Visual Studio 2012 Update 3. 我在Visual Studio 2012 Update 3中创建了一个MVC 4项目,比如MyProj.Mvc

I followed the suggestion here to put my GlobalResources.resx in a Resources folder under the project, so that it will compile into the DLL. 我按照这里的建议将我的GlobalResources.resx放在项目下的Resources文件夹中,以便它将编译到DLL中。 I have looked at the generated code in GlobalResources.resx.cs , and everything is public. 我查看了GlobalResources.resx.cs生成的代码,一切都是公开的。 I can access the resources from other .cs files (eg HomeController.cs ) just fine. 我可以从其他.cs文件(例如HomeController.cs )访问资源就好了。

I cannot get the MyProj.Mvc namespace to be in the current context in any view. 我无法在任何视图中将MyProj.Mvc命名空间置于当前上下文中。

For instance, in any Razor view, I can reference a resource like this just fine: 例如,在任何Razor视图中,我可以引用这样的资源就好了:

@MyProj.Mvc.Resources.GlobalResources.AppName

However, when I try to do the following: 但是,当我尝试执行以下操作时:

@Resources.GlobalResources.AppName

I get an error. 我收到一个错误。 The Resources namespace is not found. 找不到Resources命名空间。

I followed the advice here and put the namespace in my ~/Views/Web.config: 我按照这里的建议将命名空间放在〜/ Views / Web.config中:

<add namespace="MyProj.Mvc" />

And, the view still cannot recognize the Resources namespace. 并且,视图仍然无法识别Resources命名空间。 Rebuilding and/or restarting Visual Studio does not work. 重建和/或重新启动Visual Studio不起作用。

"Ok," I think, "screw it, I'll just put a @using statement at the top of the page and deal with it later." “好吧,”我想,“搞砸了,我只是在页面顶部放置一个@using语句并稍后处理它。”

@using MyProj.Mvc // put at the top of the page

And, the view still cannot recognize the Resources namespace. 并且,视图仍然无法识别Resources命名空间。

So please, help. 所以,请帮助。 Help! 救命! What could I possibly be doing wrong, that putting a @using statement at the top of the page still doesn't bring the namespace into scope? 我可能做错了@using ,在页面顶部放置@using语句仍然没有将命名空间纳入范围?

Steps to reproduce: 重现步骤:

I've been able to reproduce this with brand-new, clean projects. 我已经能够通过全新的清洁项目重现这一点。 Visual Studio 2012 Update 3, create a new ASP.NET MVC 4 Web Application called MyProj.Mvc . Visual Studio 2012 Update 3,创建一个名为MyProj.Mvc的新ASP.NET MVC 4 Web应用程序。 For the template, I'm mostly concerned with the Internet Application, though I've also reproduced this on my machine with the Empty project; 对于模板,我主要关注的是Internet应用程序,虽然我也在我的机器上使用Empty项目重现了它; select the Internet Application just to better match what I have. 选择Internet应用程序只是为了更好地匹配我所拥有的。 At the top of _Layout.cshtml , put @using MyProj.Mvc . _Layout.cshtml的顶部,放置@using MyProj.Mvc Somewhere else on the page, begin typing @Controllers.HomeController . 在页面的其他地方,开始输入@Controllers.HomeController Note that the auto-complete doesn't even attempt to offer up the Controllers namespace, let alone the HomeController . 请注意,自动完成甚至不会尝试提供Controllers命名空间,更不用说HomeController

Now it gets weird: 现在它变得奇怪了:

So, that failed. 那么,失败了。 Here's something, though. 不过,这是件好事。 Try changing the using statement to @using MyProj.Mvc.Controllers . 尝试将using语句更改为@using MyProj.Mvc.Controllers Now, go back and start typing @HomeController . 现在,返回并开始输入@HomeController IT WORKS! 有用!

So, what makes the project's namespace so special, that it refuses to come into scope in the views??? 那么,是什么让项目的命名空间如此特殊,以至于它拒绝进入视图中的范围?

I think you misunderstand how to access namespaces. 我认为你误解了如何访问命名空间。

Just because you reference a namespace in your code it doesn't mean that you can directly access that namespace's namespaces. 仅仅因为您在代码中引用了命名空间,并不意味着您可以直接访问该命名空间的命名空间。

For example, try this: 例如,试试这个:

Make a new C# class called MyDemoClass like this: 创建一个名为MyDemoClass的新C#类,如下所示:

using System.Web;

public class MyDemoClass
{
    Mail.MailMessage // can't do this
}

Notice that I know that System.Web.Mail is a namespace and I know I can access it. 请注意,我知道System.Web.Mail是一个命名空间,我知道我可以访问它。 But just because I'm using System.Web doesn't mean that I can jump into my code and type Mail.<etc> . 但仅仅因为我using System.Web并不意味着我可以跳转到我的代码并输入Mail.<etc> The compiler doesn't know what I want. 编译器不知道我想要什么。 Luckily it's smart enough to suggest 幸运的是,它很聪明,可以提出建议

Do you mean:
    System.Net.Mail
    System.Web.Mail

That's why you can using MyProj.Mvc.Controllers and access your controller classes but you can't simply using MyProj.Mvc and then try to access the namespace Controllers from there. 这就是为什么你可以using MyProj.Mvc.Controllers并访问你的控制器类,但你不能简单地using MyProj.Mvc然后尝试从那里访问名称空间Controllers It doesn't work. 它不起作用。

This is also why you see a lot of this: 这也是你看到很多这个的原因:

using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

Instead of this: 而不是这个:

using System;
using Web;
using Mvc;
using Html;

It feels redundant, I know, but it's the way things are. 我知道,这感觉多余,但事情就是如此。

Try making sure the very last namespace is referenced. 尝试确保引用最后一个名称空间。 If Resources is a namespace then you'll need to using it or add it in the Web.config . 如果Resources是命名空间,那么您将需要using它或将其添加到Web.config

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM