dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
34.79k stars 9.83k forks source link

direct communication between razor and js in razor files #49582

Open sajjadarashhh opened 11 months ago

sajjadarashhh commented 11 months ago

we can create feature for communicate between js and c# directly in razor pages with some senarios that make easier to coding for developers and thats not effect performance because that can be handled in compile time

Describe the solution you'd like

<script>
@size = document.getElementById('test').width;
</script>
@code {
  public int size{get;set}
}

js compiled to this:

<script>
DotNetRef.size=document.getElementById('test').width;
</script>

Additional context

this make easier to use js modules and another js features in c# very easy

sajjadarashhh commented 10 months ago

I have another idea for that We can create new keyword named: script And write c# and js in that like this:

@script{
  @size = document.getElementById(size).innerText:
}
sajjadarashhh commented 10 months ago

@danroth27 can you help me for this?

marinasundstrom commented 10 months ago

@sajjadarashhh I don't think that this is in line with what Razor is about. You are suggesting adding JavaScript-specific syntactic sugar into the Razor templating language.

There are things that seriously would make this a bad and confusing experience in that it makes you mix two languages, C#/Razor/.NET and JavaScript, that don't execute in the same domain.

Then we have the fact that JavaScript can't execute everywhere or at any stage of a component's lifetime, like when the component is being pre-rendered on the server.

sajjadarashhh commented 10 months ago

@marinasundstrom i dont said we must be take that in this way I just have an idea to make a way to use js and .net in new format that make it easier to use c#-js interoperability we can choose many ways for this something like new files depend on components that do this for us With new format something like jasor and write js functions that have access to c# objects in that component and use that functions in component For many use cases developers want to configure js components easier with out write many codes for interop And we can send blazor components to js libraries without writing many codes just with c# element refrences This take razor more easier and developer friendly

marinasundstrom commented 10 months ago

@sajjadarashhh JS and C# each have some concepts that don't work well together. JavaScript is a dynamic language with no extensive type structure that is even compatible with C#/.NET. And blending the languages would arguably just make it more confusing for developers. That is why it has failed so many times. So we have to respect the boundaries between the different environments.

C# interoperability with JavaScript in Blazor is already quite good. In Blazor WebAssembly you can use source generators that generate C#/JS interop code for you. It is similar to how P/Invoke does it with native code.

https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/import-export-interop?view=aspnetcore-7.0

I used to think that proposal like these were great - so I sympathize. Nowadays, I realize that interoperability is far more complex.

There has been some projects attempting to generate C# interop code from TypeScript definitions files I know that Mono did experiment with it. Though these project have not caught on. Mainly because the experience has not been that great and the demand low. Perhaps also because of lack of exposure.

sajjadarashhh commented 10 months ago

The goals of this idea is not thing we create a way to work c# and js working together The goal of this is a create a way to make interop codes at compile and automatically Each thing interop doing this pattern can do that And this is not very complex as you discussed @marinasundstrom

marinasundstrom commented 10 months ago

@sajjadarashhh I think that this would essentially be a new a language in itself. Trying to blur the lines between JS and C#.

Razor is basically a templating language on top of C#/.NET. Not an interop tool.

ghost commented 10 months ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

sajjadarashhh commented 10 months ago

no this can be a optional feature in razor that can be enabled or disabled

ghost commented 7 months ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.