Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Templated Razor Delegates within a @section = '__razor_template_writer': a parameter or local variable cannot have the same name as a method type parameter #672

Closed
nikoszs opened this issue Jan 21, 2016 · 2 comments
Assignees
Milestone

Comments

@nikoszs
Copy link

nikoszs commented Jan 21, 2016

I'm having problems creating templated razor delegates within a section of a page.

_Layout.chtml

@RenderSection("test", false)

_SubLayout.cshtml

@section test {
    @ { Func<dynamic, object> f = @<span>@item</span>; }
}

Which gives me this error:

CS0412 '__razor_template_writer': a parameter or local variable cannot have the same name as a method type parameter

So I went to look at what gets generated, and this is it

Func<dynamic, Microsoft.AspNet.Mvc.Razor.HelperResult> e = 
    item => new Microsoft.AspNet.Mvc.Razor.HelperResult(async (__razor_template_writer) =>
    {
        BeginContext(2881, 6, true);
        WriteLiteralTo(__razor_template_writer, "<span>");
        EndContext();
        BeginContext(2888, 4, false);
        WriteTo(__razor_template_writer, item);
        EndContext();
        BeginContext(2893, 7, true);
        WriteLiteralTo(__razor_template_writer, "</span>");
        EndContext();
    });

So the error makes sense, in the section there's already a method parameter called __razor_template_writer when a section is created, and now by creating a templated razor delegate, it tried to create another '__razor_template_write' parameter, hence the error.

I then copied the above code to my view just changed the method parameter name from __razor_template_writer to __test in the above function and called @e("this is a test"). It works.

I tested the initial problem code in MVC5 and works fine, so my question is, is this a bug?

It smells like one. And also, is there a way around it?

@dougbu
Copy link
Member

dougbu commented Jan 21, 2016

@alzdrak I suspect __razor_template_writer will be used as a lambda parameter for both the section (@section test { ... }) and the template (@<span>@item</span>). Further the two lambdas should nest in the generated code. Could you check the @section code and its relation to the block you pasted above to confirm? Uploading the whole file or placing it in a gist may help as well.

Anything that avoids templates w/in a section (such as your @e("this is a test")) should work around the issue.

@NTaylorMullen
Copy link
Member

6bad1bb

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants