Sharing Code and Resources
Setting Up The API
Only Blazor WebAssembly
requires access to the Web API because it lacks direct access to the database.
For Blazor Server, it's likely that the most prevalent architecture involves
utilizing a Web API as well. We need to transfer the files we can share into a
different library. To accomplish this, please follow these steps:
- Right-click on the MyBlog solution and select Add | New Project.
- Search for Class Library (.NET Core) and then click Next.
- Name the project MyBlog.Data.Shared and keep the location as is and then click Create.
- Select target framework .NET 5.0 (Current) and then click Create.
- Right-click on the Dependencies node under the MyBlogWebAssembly.Client project. Click Add project reference, check the MyBlog.Data.Shared and MyBlog.Shared checkboxes, and then click OK.
- Move the following files
from the MyBlog.Data project to MyBlog.Data. Shared:
Extension -
folder
Interfaces - folder
Models/BlogPost.cs
Models/Category.cs
Models/Tag.cs
MyBlogApiClientSide.cs
These are the files that
are the same regardless of the hosting model.
- We need to add some NuGet Packages to get things working. Right-click in the Dependencies node under the MyBlog.Data.Shared project and select Manage NuGet Packages.
- Search for Newtonsoft.Json and then click Install.
- Search for Microsoft.AspNetCore.Components.WebAssembly. Authentication and then click Install.
- Search for Microsoft.Extensions.Http and then click Install.
- Now we need to reference the new project. Right-click on the Dependencies node under the MyBlog.Data project and click Add project reference.
- Check MyBlog.Data.Shared and then click OK.
- Right-click on the Dependencies node under the MyBlog.Shared project and then click Add project reference.
- Check MyBlog.Data.Shared and then click OK.
Now we have moved the
classes that are shareable to a new library.
Moving The Components
Initially, we need to set
up a new project and transfer some files. To achieve this, follow the steps
below:
- Right-click on the MyBlog solution and select Add | New project.
- Search for Razor and you should find a template called Razor Class Library. Select that template and click Next.
- Name the project MyBlog.Shared, leave the location as is (it should be in the correct folder already), and then click Next.
- Select Target Framework .NET 5.0 (Current) and make sure Support pages and views are unchecked. Then, click Create.
- Add a reference to the MyBlog.Data.Shared project by right-clicking the Dependencies node under the MyBlog.Shared project and selecting Add project reference. Check the MyBlog.Data.Shared checkbox and then click OK.
- Add a NuGet package by right-clicking Dependencies under the MyBlog.Shared node and selecting Manage NuGet Packages.
- Search for Markdig and then click Install.
- Tick the Include pre-release box. Search for Microsoft.AspNetCore.Components.Web.Extensions and then click Install. Untick the Include pre-release box.
- Search for Microsoft.AspNetCore.Components.WebAssembly. Authentication and then click Install.
- Now we need to add some namespaces. Open the _Imports.razor file and replace the content.
- Right-click on the MyBlog.Shared project and then select Add | New Folder. Name the folder Components.
- Now, move all the files except LoginDisplay.razor from the Components folder in the MyBlogServerSide project to the Components folder in the MyBlog.Shared project. The Login display differs a bit depending on the hosting platform, so we don't want to share that one.
- Right-click on the MyBlog.Shared project and then select Add | New Folder. Name the folder Pages.
- Move the Admin folder from the Pages folder in the MyBlogServerSide project to the Pages folder in the MyBlog.Shared project.
- Move the Index.razor and Post.razor files from the Pages folder in the MyBlogServerSide project to the Pages folder in the MyBlog.Shared project.
- Right-click on the MyBlog.Shared project and then select Add | New Folder. Name the folder Shared.
- Move the NavMenu.razor file from the Shared folder in the MyBlogServerSide project to the Shared folder in the MyBlog.Shared project.
- Add a reference to the MyBlog.Shared project by right-clicking the Dependencies node under the MyBlogServerSide project and selecting Add project reference. Check the MyBlog.Shared checkbox and then click OK.
Now we have moved all the
files we want to share between the projects and configured the MyBlogServerSide
project.
Conclusion
We have successfully set
up an API and moved all the files from the shared folder.
Comments
Post a Comment