Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler error with mixed content in onclick attribute #5605

Closed
panda-eye opened this issue Oct 6, 2018 · 5 comments
Closed

Compiler error with mixed content in onclick attribute #5605

panda-eye opened this issue Oct 6, 2018 · 5 comments
Labels
affected-very-few This issue impacts very few customers area-blazor Includes: Blazor, Razor Components area-razor.compiler This issue is related to the Razor compiler (now external) bug This issue describes a behavior which is not expected - a bug. feature-razor.language ✔️ Resolution: Duplicate Resolved as a duplicate of another issue reevaluate We need to reevaluate the issue and make a decision about it severity-minor This label is used by an internal tool Status: Resolved
Milestone

Comments

@panda-eye
Copy link

I have a line in Teachers.cshtml:
<button onclick="window.location.href = '/teacher/@(teacher.Id)/';">More info...</button>
After compilation I have the following line in Teachers.g.cs (/obj/Debug/netstandart2.0/Pages/):
builder.AddAttribute(24, "onclick", (Microsoft.AspNetCore.Blazor.Components.BindMethods.GetEventHandlerValue<Microsoft.AspNetCore.Blazor.UIMouseEventArgs>() + "window.location.href" + " =" + " \'/teacher/" + (teacher.Id) + "/\';" + ()));
And an error message:

CS1525 Invalid expression term ")"

@panda-eye
Copy link
Author

The same result if I remove whitespaces

@codeinstinct
Copy link

codeinstinct commented Oct 6, 2018

Here's what I would do:

Change this
<button onclick="window.location.href = '/teacher/@(teacher.Id)/';">More info...</button>

to
<button onclick=@Navigate(teacher.Id)>More info...</button>

Add this line at the top of the .cshtml (after the @page "<route>" directive)
@inject Microsoft.AspNetCore.Blazor.Services.IUriHelper UriHelper

Add this method Navigate to the functions block

@functions {
    private void Navigate(string id)
    {
        UriHelper.NavigateTo($"/teacher/{id}");
    }
}

@ghost
Copy link

ghost commented Oct 6, 2018

Believe this issue might be a similar problem with double and single quote combinations not being correctly escaped as raised in issue #1527

@SteveSandersonMS
Copy link
Member

Thanks for reporting this, @panda-eye.

I think it's a separate issue from #1527, because it can be reproduced without involving any quotes, e.g.:

@{ var a = "a"; }
<button onclick="@(a)b">More info...</button>

Codegen output:

// Compiler error due to empty parens:
builder.AddAttribute(1, "onclick", (Microsoft.AspNetCore.Blazor.Components.BindMethods.GetEventHandlerValue<Microsoft.AspNetCore.Blazor.UIMouseEventArgs>(a) + "b" + ()));

Looks like we don't correctly handle mixed content within event handler attributes at the moment.

Workaround: Use C# string interpolation instead, e.g.:

<button onclick=@($"window.location.href = '/teacher/{teacher.Id}/';")>

Adding to backlog.

@SteveSandersonMS SteveSandersonMS changed the title An error while parsing .cshtml Compiler error with mixed content in onclick attribute Oct 8, 2018
@aspnet-hello aspnet-hello transferred this issue from dotnet/blazor Dec 17, 2018
@aspnet-hello aspnet-hello added this to the Backlog milestone Dec 17, 2018
@aspnet-hello aspnet-hello added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates bug This issue describes a behavior which is not expected - a bug. area-blazor Includes: Blazor, Razor Components labels Dec 17, 2018
@mkArtakMSFT mkArtakMSFT removed area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates labels May 9, 2019
@SteveSandersonMS SteveSandersonMS added affected-very-few This issue impacts very few customers severity-minor This label is used by an internal tool labels Oct 14, 2020 — with ASP.NET Core Issue Ranking
@javiercn javiercn added area-razor.compiler This issue is related to the Razor compiler (now external) reevaluate We need to reevaluate the issue and make a decision about it feature-razor.language labels Apr 19, 2021
@TanayParikh
Copy link
Contributor

Closing as dup of dotnet/razor#7684

@TanayParikh TanayParikh added the ✔️ Resolution: Duplicate Resolved as a duplicate of another issue label Oct 19, 2021
@ghost ghost added the Status: Resolved label Oct 19, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Nov 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affected-very-few This issue impacts very few customers area-blazor Includes: Blazor, Razor Components area-razor.compiler This issue is related to the Razor compiler (now external) bug This issue describes a behavior which is not expected - a bug. feature-razor.language ✔️ Resolution: Duplicate Resolved as a duplicate of another issue reevaluate We need to reevaluate the issue and make a decision about it severity-minor This label is used by an internal tool Status: Resolved
Projects
None yet
Development

No branches or pull requests

7 participants