3

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.

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. I have looked at the generated code in GlobalResources.resx.cs, and everything is public. I can access the resources from other .cs files (e.g. HomeController.cs) just fine.

I cannot get the MyProj.Mvc namespace to be in the current context in any view.

For instance, in any Razor view, I can reference a resource like this just fine:

@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.

I followed the advice here and put the namespace in my ~/Views/Web.config:

<add namespace="MyProj.Mvc" />

And, the view still cannot recognize the Resources namespace. Rebuilding and/or restarting Visual Studio does not work.

"Ok," I think, "screw it, I'll just put a @using statement at the top of the page and deal with it later."

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

And, the view still cannot recognize the Resources namespace.

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?

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. For the template, I'm mostly concerned with the Internet Application, though I've also reproduced this on my machine with the Empty project; select the Internet Application just to better match what I have. At the top of _Layout.cshtml, put @using MyProj.Mvc. Somewhere else on the page, begin typing @Controllers.HomeController. Note that the auto-complete doesn't even attempt to offer up the Controllers namespace, let alone the HomeController.

Now it gets weird:

So, that failed. Here's something, though. Try changing the using statement to @using MyProj.Mvc.Controllers. Now, go back and start typing @HomeController. IT WORKS!

So, what makes the project's namespace so special, that it refuses to come into scope in the views???

2
  • Did you try moving the resource to App_GlobalResources folder? And then access it using @TestApp.App_GlobalResources.Resource1 Commented Aug 6, 2013 at 2:06
  • The first link I gave suggested not using the App_GlobalResources folder since it would cause issues with unit tests. Commented Aug 6, 2013 at 2:25

1 Answer 1

1

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:

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. But just because I'm using System.Web doesn't mean that I can jump into my code and type 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. 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.

Sign up to request clarification or add additional context in comments.

3 Comments

Then why can I access Resources.GlobalResources.AppName from HomeController? At any rate, at least you've let me realize that I should just do what you said, and reference MyProj.Mvc.Resources in my ~/Views/Web.config and reference my strings that way. I could've sworn that I had seen it referenced as Resources.GlobalStrings.AppName (or equivalent) somewhere, but it's not worth wasting my time on it, especially if I'm trying to save keystrokes anyway.
What namespace did you use for your GlobalResources.resx? If you use Resources.GlobalResources you can access this automatically in Razor with @Resources.GlobalResources but not if you set the namespace to MyProj.Mvc.Resources.GlobalResources.
The latter, MyProj.Mvc.Resources.GlobalResources. Perhaps the examples I had seen had the resource class under the Resources namespace as you suggest. It's just weird that I can refer to it as Resources.GlobalResources.<resource-name> within the HomeController just fine. Perhaps it has something to do with it being a sibling namespace to the current scope (HomeController being under MyProj.Mvc.Controllers, sibling to Myproj.Mvc.Resources)? I suppose this would make sense, as Razor views probably aren't scoped within a namespace, so wouldn't have any sibling namespaces.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.