code generated from partial view has the wrong name

Dec 17, 2012 at 10:32 PM

Hi,

Thanks for the great tool.

I use a name convention whereby my partial view names start with an '_'.

The class generated from the view doesn't include the underscore, which eventually leads to a name clash with the full view's generated class name.

For example 'Details' is usually implemented in two views: 'Details.cshtml' and '_Details.cshtml'; the classes in the generated file are both wrongly named 'Details'.

How do I overcome this 'tiny' issue? (see generated code below)

[System.CodeDom.Compiler.

GeneratedCodeAttribute("RazorGenerator", "1.5.0.0" )] [System.Web.WebPages.PageVirtualPathAttribute("~/Areas/MetaDataAdmin/Views/DisplayLayout/_Details.cshtml" )]
public class Details : System.Web.Mvc.WebViewPage<DisplayLayout>

 

Thanks

Coordinator
Dec 17, 2012 at 10:40 PM

Hmmm, not sure why the underscore is getting stripped. Seems underscores in the middle and end of the name are preserved, but those in the beginning are trimmed. Pranav?

Coordinator
Dec 17, 2012 at 11:15 PM
Edited Dec 17, 2012 at 11:16 PM

The WebPages administrator pages (which was sort of the original reason this tool was written) was producing warnings about names that weren't CLR compliant when they had leading underscores which is why it trims by default. There's a directive you could add to make it not do this

At the top of your cshtml file,

@* TrimLeadingUnderscores: false *@

Dec 17, 2012 at 11:45 PM

Hi pranavkm,

Thanks I will try that (even though I have to modify scores of views)

Does CLR non-compliance imply name clashes with Framework names?

Thanks again

Coordinator
Dec 18, 2012 at 12:47 AM

You can add a single file named "razorgenerator.directives to the root of your views directory with the attribute and that should solve it:

// razorgenerator.directives

TrimLeadingUnderscores: false

Coordinator
Dec 18, 2012 at 12:55 AM

To answer your other question, no. The assembly that had precompiled Razor files was part of the Mvc \ Asp.Net Web Stack framework and was consequently marked as CLS Complaint (http://stackoverflow.com/questions/570452/what-is-clscompliant-attribute-in-net). Trimming names by default was a cop out since it meant I could get away with not changing a dozen or so files. I should probably reverse it :)

May 30, 2013 at 11:16 AM
Brilliant!I was having the same problem, and getting some very odd problems with partial views in Areas sometimes not being found, possibly because they're lost in the cache, possibly because they're being sought with the underscore but exist without it.

I don't have a lot of evidence, besides a little observation, but reinstating the underscores seems to fix it.

I'm delighted that there's such a simple solution, and that this post was here to keep my downtime under 10 minutes!
May 30, 2013 at 12:31 PM
Edited May 30, 2013 at 2:48 PM
Sorry, disregard the above. I'm still overjoyed that razorgenerator.directives allows me to keep the leading underscores in my generated class names, but the part about this helping with my problem is not true. I can work around it by setting UsePhysicalViewsIfNewer = false, but if I don't set that and the PartialView is newer, the ViewEngine won't find it.

I'm using a customised PrecompiledViewEngine so I'll lay the blame at my own feet.