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

Page is missing some APIs that Razor calls #5920

Closed
rynowak opened this issue Mar 7, 2017 · 3 comments
Closed

Page is missing some APIs that Razor calls #5920

rynowak opened this issue Mar 7, 2017 · 3 comments

Comments

@rynowak
Copy link
Member

rynowak commented Mar 7, 2017

Typing in this code will result in the following compiler error:

@page
<h2><a asp-route-page="Show" asp-route-id="@post.Id">@post.Title</a></h2>

post.Id is an int

The name 'InvalidTagHelperIndexerAssignment' does not exist in the current context'


This is happening because Razor tries to generate a call to the InvalidTagHelperIndexerAssignment method to throw an exception if the route values aren't set.

It doesn't happen with asp-route-page because the optimizer preallocates it.


Generated code:

#pragma checksum "/Pages/Index.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "3b1862329315e3e3a822c78cbcef8e944fc7cc65"
namespace AspNetCore
{
    #line hidden
    using System;
    using System.Threading.Tasks;
#line 2 ""
using System.Linq;

#line default
#line hidden
#line 3 ""
using System.Collections.Generic;

#line default
#line hidden
#line 4 ""
using Microsoft.AspNetCore.Mvc;

#line default
#line hidden
#line 5 ""
using Microsoft.AspNetCore.Mvc.Rendering;

#line default
#line hidden
#line 6 ""
using Microsoft.AspNetCore.Mvc.ViewFeatures;

#line default
#line hidden
#line 1 "/Pages/Index.cshtml"
using PagesBlog;

#line default
#line hidden
#line 2 "/Pages/Index.cshtml"
using PagesBlog.Models;

#line default
#line hidden
    public class _Pages_Index_cshtml : global::Microsoft.AspNetCore.Mvc.RazorPages.Page
    {
        private static readonly global::Microsoft.AspNetCore.Razor.TagHelpers.TagHelperAttribute __tagHelperAttribute_0 = new global::Microsoft.AspNetCore.Razor.TagHelpers.TagHelperAttribute("asp-route-page", "Show", global::Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeValueStyle.DoubleQuotes);
        #line hidden
        #pragma warning disable 0414
        private string __tagHelperStringValueBuffer = null;
        #pragma warning restore 0414
        private global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext __tagHelperExecutionContext = null;
        private global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner __tagHelperRunner = new global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner();
        private global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperScopeManager __backed__tagHelperScopeManager = null;
        private global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperScopeManager __tagHelperScopeManager
        {
            get
            {
                if (__backed__tagHelperScopeManager == null)
                {
                    __backed__tagHelperScopeManager = new Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperScopeManager(StartTagHelperWritingScope, EndTagHelperWritingScope);
                }
                return __backed__tagHelperScopeManager;
            }
        }
        private global::Microsoft.AspNetCore.Mvc.TagHelpers.AnchorTagHelper __Microsoft_AspNetCore_Mvc_TagHelpers_AnchorTagHelper = null;
        #pragma warning disable 1998
        public async override global::System.Threading.Tasks.Task ExecuteAsync()
        {
            BeginContext(7, 2, true);
            WriteLiteral("\r\n");
            EndContext();
            BeginContext(88, 2, true);
            WriteLiteral("\r\n");
            EndContext();
#line 7 "/Pages/Index.cshtml"
   
    var posts = DbContext.Posts.ToArray();


#line default
#line hidden
            BeginContext(180, 2, true);
            WriteLiteral("\r\n");
            EndContext();
            BeginContext(493, 72, true);
            WriteLiteral("\r\n<div class=\"row\">\r\n\r\n    <p>Welcome to my blog!</p>\r\n    <p>There are ");
            EndContext();
            BeginContext(566, 12, false);
#line 27 "/Pages/Index.cshtml"
            Write(posts.Length);

#line default
#line hidden
            EndContext();
            BeginContext(578, 14, true);
            WriteLiteral(" posts</p>\r\n\r\n");
            EndContext();
#line 29 "/Pages/Index.cshtml"
     for (var i = 0; i < posts.Length; i++)
    {
        var post = posts[i];



#line default
#line hidden
            BeginContext(676, 35, true);
            WriteLiteral("        <article>\r\n            <h2>");
            EndContext();
            __tagHelperExecutionContext = __tagHelperScopeManager.Begin("a", global::Microsoft.AspNetCore.Razor.TagHelpers.TagMode.StartTagAndEndTag, "6d374b9034664211887f9dec2fb8d699", async() => {
                BeginContext(772, 10, false);
#line 34 "/Pages/Index.cshtml"
                                                                       Write(post.Title);

#line default
#line hidden
                EndContext();
            }
            );
            __Microsoft_AspNetCore_Mvc_TagHelpers_AnchorTagHelper = CreateTagHelper<global::Microsoft.AspNetCore.Mvc.TagHelpers.AnchorTagHelper>();
            __tagHelperExecutionContext.Add(__Microsoft_AspNetCore_Mvc_TagHelpers_AnchorTagHelper);
            __Microsoft_AspNetCore_Mvc_TagHelpers_AnchorTagHelper.RouteValues["page"] = (string)__tagHelperAttribute_0.Value;
            __tagHelperExecutionContext.AddTagHelperAttribute(__tagHelperAttribute_0);
            if (__Microsoft_AspNetCore_Mvc_TagHelpers_AnchorTagHelper.RouteValues == null)
            {
                throw new InvalidOperationException(InvalidTagHelperIndexerAssignment("asp-route-id", "Microsoft.AspNetCore.Mvc.TagHelpers.AnchorTagHelper", "RouteValues"));
            }
            BeginWriteTagHelperAttribute();
#line 34 "/Pages/Index.cshtml"
                                           WriteLiteral(post.Id.ToString());

#line default
#line hidden
            __tagHelperStringValueBuffer = EndWriteTagHelperAttribute();
            __Microsoft_AspNetCore_Mvc_TagHelpers_AnchorTagHelper.RouteValues["id"] = __tagHelperStringValueBuffer;
            __tagHelperExecutionContext.AddTagHelperAttribute("asp-route-id", __Microsoft_AspNetCore_Mvc_TagHelpers_AnchorTagHelper.RouteValues["id"], global::Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeValueStyle.DoubleQuotes);
            BeginContext(711, 75, false);
            await __tagHelperRunner.RunAsync(__tagHelperExecutionContext);
            if (!__tagHelperExecutionContext.Output.IsContentModified)
            {
                await __tagHelperExecutionContext.SetOutputContentAsync();
            }
            Write(__tagHelperExecutionContext.Output);
            __tagHelperExecutionContext = __tagHelperScopeManager.End();
            EndContext();
            BeginContext(786, 22, true);
            WriteLiteral("</h2>\r\n            <p>");
            EndContext();
            BeginContext(809, 20, false);
#line 35 "/Pages/Index.cshtml"
          Write(Summarize(post.Text));

#line default
#line hidden
            EndContext();
            BeginContext(829, 26, true);
            WriteLiteral("</p>\r\n        </article>\r\n");
            EndContext();
#line 37 "/Pages/Index.cshtml"
    }


#line default
#line hidden
            BeginContext(862, 10, true);
            WriteLiteral("\r\n</div>\r\n");
            EndContext();
        }
        #pragma warning restore 1998
#line 11 "/Pages/Index.cshtml"
            
    public string Summarize(string text, int length = 50)
    {
        if (text.Length <= length)
        {
            return text;
        }

        var position = text.LastIndexOf(' ', length - 1);
        return text.Substring(position == -1 ? length : position) + "...";
    }


#line default
#line hidden
        [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
        public ApplicationContext DbContext { get; private set; }
        [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
        public Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider ModelExpressionProvider { get; private set; }
        [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
        public Microsoft.AspNetCore.Mvc.IUrlHelper Url { get; private set; }
        [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
        public Microsoft.AspNetCore.Mvc.IViewComponentHelper Component { get; private set; }
        [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
        public Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json { get; private set; }
        [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
        public Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<_Pages_Index_cshtml> Html { get; private set; }
        public global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<_Pages_Index_cshtml> ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<_Pages_Index_cshtml>)PageContext?.ViewData;
        public _Pages_Index_cshtml Model => ViewData.Model;
    }
}

@rynowak
Copy link
Member Author

rynowak commented Mar 7, 2017

IMO the best solution for this would be to bake this into Razor.Runtime and codegen to that, but it probably deserves some discussion.

@rynowak
Copy link
Member Author

rynowak commented Mar 7, 2017

assigned @ryanbrandenburg - this is a class 2 adoption blocker. @pranavkm can help you figure out the best fix, we might be able to move this down to the base class. Please figure out if more changes are needed while you are there, and add a functional test 👍

@rynowak
Copy link
Member Author

rynowak commented Mar 7, 2017

Adding this to @functions works around the problem:

    public string InvalidTagHelperIndexerAssignment(string foo, string bar, string baz)
    {
        return "error";
    }

Note that you will get a warning from tooling, because it is confused about the base-class.

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

2 participants