Bug with namespace and using statements in generated code

Feb 4, 2014 at 11:45 PM
Edited Feb 4, 2014 at 11:48 PM
Let me start by saying this is an amazing project which allows my codebase to be much cleaner by separating appropriate views into different assemblies. Great job!

I've come across the following problem quite a bit because we have a large codebase with many namespaces. I have a class in my project in the following namespace, like so:

namespace Views.MyHelpers
{
    public class Helper
    {
        
    }
}
Consider the following code in a Razor page inside a Views folder that includes this namespace:
@using Views.MyHelpers
And the generated file looks like this:

namespace Scratch3.Views
{
   
....    
    #line 2 "..\..\Views\WebPage1.cshtml"
    using Views.MyHelpers;   // this line errors, it can't find Views.MyHelpers
...
This causes a compiler error because the using statement is inside the namespace, and as such it is actually looking in Scratch3.Views.MyHelpers, which doesn't exist and the compiler errors out.

An article about how "using" statements are treated when inside namespaces:
http://stackoverflow.com/questions/125319/should-usings-be-inside-or-outside-the-namespace

Moving the using statement outside the namespace makes it compile, but of course now I'm hand modifying a generated file. You can easily reproduce this with one view and one helper class in an MVC project, with the appropriate namespaces.

Can the generator put the usings outside the namespace? How can we fix this behavior? Thanks!
Coordinator
Feb 5, 2014 at 1:39 AM
Note that RazorGenerator is not really generating the code from scratch, but is actually calling into the real Razor runtime (part of ASP.NET) to do the generation. So it would be interesting to understand whether the same issue might exist without RZ, when your views are compiled dynamically at runtime.
Feb 5, 2014 at 4:15 PM
These files have been running the normal way for years, so it's not a problem the traditional way. I found the generated files in the Temporary ASP.NET folders, the generated file puts everything in the namespace "ASP" .

namespace ASP {
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
....
Then the class name contains the full path name :
public class _Page_Views_Search_Criteria__Specialty_cshtml
And there's no PageVirtualPathAttribute. Did you make these custom changes to the generator? Or are there flags in the generator API that can get this behavior?
Oct 29, 2014 at 11:08 PM
Edited Oct 29, 2014 at 11:26 PM
If I may, I would like to upvote this issue because it has created name conflict errors for us. With the using directives being placed inside the namespace declaration, this precludes the use of any of the included namespaces in our own code. For example:
namespace My.Space.Views.Reports.MyReport {
    using System;
    using System.IO;

    ....
}
This will generate missing assembly reference errors on the "using System.IO" line when we have another view called My.Space\Views\System\Users.cshtml. In this case, the "using System" directive forces the compiler to first search within My, My.Space, My.Space.Views, and My.Space.Views.Reports, finding My.Space.Views.System. The subsequent directive beginning with System does the same, attempting to locate the non-existent My.Space.Views.System.IO namespace.