JavaScriptServices icon indicating copy to clipboard operation
JavaScriptServices copied to clipboard

Use Razor/CSHTML page instead of static HTML

Open aherrick opened this issue 9 years ago • 8 comments

Is it possible to leverage a CSHTML Razor page as the HTML source of a component?

This would be instead of the static HTML file associated with the component by default.

aherrick avatar Jul 11 '17 12:07 aherrick

By "component", do you mean an Angular/React component? If so no, that's not possible, because these components get compiled at build time into a static .js file, so there's no way of running .NET code inside it.

SteveSandersonMS avatar Jul 11 '17 13:07 SteveSandersonMS

Hi Steve. Not what I mean.

For Angular I want to to point my template url source to an ASP.NET route (/angular/page1) and it to render that "View" as the HTML for the component. I know this was possible in AngularJS

https://stackoverflow.com/questions/25785579/is-angular-routing-template-url-support-for-cshtml-file-in-asp-net-mvc-5-projec

Thanks!

aherrick avatar Jul 12 '17 14:07 aherrick

Yes, that's exactly what I thought you meant! And no, I'm afraid Angular 2+ is completely different, in that it's not loading templates as strings from HTTP requests at runtime.

It might be possible to hook into Angular 2 somehow to make it do this, but it's not something I've tried, and would certainly give up on the benefits of AoT compilation.

SteveSandersonMS avatar Jul 12 '17 14:07 SteveSandersonMS

Ah... appreciate the response. Sure would be lovely to have that server side power at run time :)

aherrick avatar Jul 12 '17 15:07 aherrick

Hi, since I am trying the same thing now, have you found a way-around? Or what further steps will you take @aherrick ? Appreciate your response!

avffw avatar Jan 25 '18 13:01 avffw

Abandoned this approach unfortunately. Not sure if things are any different with the 2.0 release (my guess is not)

aherrick avatar Jan 25 '18 14:01 aherrick

I think the compilation process of angular 2+ is different because angular cli needs the template (as html string) or templateUrl instead to render the component. To add the razor side render power to angular (as optional) should be a nice feature in the future.

I'm not sure, but if you compile your angular application and after compiling you replace your templateUrl property value for a valid razor page (including the page and it's controller), does it works?

jesusvalenzuelar avatar Feb 28 '18 15:02 jesusvalenzuelar

Is this still not possible with Angular 6?

brunoccsnasajon avatar Nov 25 '18 22:11 brunoccsnasajon