Problem with conuming blazor custom element through another webApp!

Hamed Vaziri 136 Reputation points
2023-02-07T17:37:57.8866667+00:00

Hi everyone ..

To implement blazor server custom element that consume in a simple webApp, i've follow these steps :

  1. Create simple blazor server project with .net 7.0
  2. Register counter as custom element
  3. Enable CORS on blazor project
  4. Set autostart to false for script tag in _Host.cshtml like this : <script src="_framework/blazor.server.js" autostart="false"></script>
  5. Publish our blazor server project to local iis (this sample webSite can access through this address : http://localhost/CustomElements).
  6. Create a simple html file & add these lines :
   <html lang="en">
   <head>
       <meta charset="UTF-8">
       <meta http-equiv="X-UA-Compatible" content="IE=edge">
       <meta name="viewport" content="width=device-width, initial-scale=1.0">
       <title>Custom Elements Consuming Sample</title>
   
       <link rel="stylesheet" href="http://localhost/CustomElements/css/site.css">
       <link rel="stylesheet" href="http://localhost/CustomElements/CustomEelementSample01.styles.css">
   
   </head>
   <body>
       
       <h3>Hi Custom Elements!</h3>
       <hr />
       <my-counter />
       
       <script src="http://localhost/CustomElements/_framework/blazor.server.js"></script>
       <script src="http://localhost/CustomElements/_content/Microsoft.AspNetCore.Components.CustomElements/Microsoft.AspNetCore.Components.CustomElements.lib.module.js"></script>
       <script>
           Blazor.start();
       </script>
   
   </body>
   </html>
  1. Create new webSite in iis with html file that created in step 6 (this sample webSite can access through this address : http://localhost/CustomElementConsumer/CustomElementsConsuming.html)
  2. Open consumer web site which created in step 7. You will see the counter component was not shawn! If you check console, You will see this errors :

    GET http://localhost/CustomElementConsumer/_blazor/initializers

    Uncaught (in promise) SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

What's problem & how to solve it?

Thanks in advance

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,140 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,378 questions
{count} votes