|
1 | | -# WebExpress.Tutorial.HelloWorld |
2 | | -Tutorial of a simple hello world application for WebExpress |
| 1 | + |
| 2 | + |
| 3 | +# WebExpress |
| 4 | +`WebExpress` is a lightweight web server optimized for use in low-performance environments (e.g. Rasperry PI). By providing |
| 5 | +a powerful plugin system and a comprehensive API, web applications can be easily and quickly integrated into a .net |
| 6 | +language (e.g. C#). Some advantages of `WebExpress` are: |
| 7 | + |
| 8 | +- It is easy to use. |
| 9 | +- It offers a variety of features and tools that can help you build and manage your website. |
| 10 | +- It is fast and efficient and can help you save time and money. |
| 11 | +- It is flexible and can be customized to meet your specific requirements. |
| 12 | + |
| 13 | +The `WebExpress` family includes the following projects: |
| 14 | + |
| 15 | +- [WebExpress](https://github.com/ReneSchwarzer/WebExpress#readme) - The web server for `WebExpress` applications and the documentation. |
| 16 | +- [WebExpress.WebCore](https://github.com/ReneSchwarzer/WebExpress.WebCore#readme) - The core for `WebExpress` applications. |
| 17 | +- [WebExpress.WebUI](https://github.com/ReneSchwarzer/WebExpress.WebUI#readme) - Common templates and controls for `WebExpress` applications. |
| 18 | +- [WebExpress.WebIndex](https://github.com/ReneSchwarzer/WebExpress.WebIndex#readme) - Reverse index for `WebExpress` applications. |
| 19 | +- [WebExpress.WebApp](https://github.com/ReneSchwarzer/WebExpress.WebApp#readme) - Business application template for `WebExpress` applications. |
| 20 | + |
| 21 | +`WebExpress` is part of the `WebExpress` family. The project provides a web server for `WebExpress` applications. |
| 22 | + |
| 23 | +# Tutorial |
| 24 | +Tutorial of a simple Hello World application for WebExpress. Two projects are created for the tutorial. The `HelloWorld` project contains the functionality and all resources to run as a WebExpress application. The project `HelloWorld.App` is a helper project that is needed for debugging, testing and packaging creation. |
| 25 | + |
| 26 | +## Prerequisites |
| 27 | +- Install .NET 8.0. You can download and install .NET 8.0 from the official .NET website. Follow the instructions on the website to complete the installation (see also [installation guide](https://github.com/ReneSchwarzer/WebExpress/blob/main/doc/installation_guide.md)). |
| 28 | +- Verify the installation. Open the command line or terminal and run the following command: |
| 29 | + ```bash |
| 30 | + dotnet --version |
| 31 | + ``` |
| 32 | + This command outputs the installed .NET version. Make sure the outputted version matches the version you installed (in this case 8.0). |
| 33 | + |
| 34 | +After fulfilling these prerequisites, you can proceed with the tutorial. |
| 35 | + |
| 36 | +## Create a new solution |
| 37 | +- Open the command line or terminal. |
| 38 | + - On Windows, you can open the command line by typing cmd into the search box of the Start menu and pressing Enter. |
| 39 | + - On MacOS or Linux, you can open the terminal by typing terminal into the Spotlight search and pressing Enter. |
| 40 | +- Navigate to the desired directory. |
| 41 | +- Use the command cd `path/to/directory` to navigate to the desired directory. |
| 42 | +- Create a new solution. Enter the following command and press Enter: |
| 43 | + ```bash |
| 44 | + # create a new folder for your solution |
| 45 | + mkdir WebExpress.Tutorial.HelloWorld |
| 46 | + cd WebExpress.Tutorial.HelloWorld |
| 47 | + |
| 48 | + # create a new solution |
| 49 | + dotnet new sln -n WebExpress.Tutorial.HelloWorld |
| 50 | + |
| 51 | + # create a new console application |
| 52 | + dotnet new console -n HelloWorld.App -f net8.0 |
| 53 | + |
| 54 | + # create a new class library |
| 55 | + dotnet new classlib -n HelloWorld -f net8.0 |
| 56 | + |
| 57 | + # add the projects to the solution |
| 58 | + dotnet sln add ./HelloWorld.Add/HelloWorld.App.csproj |
| 59 | + dotnet sln add ./HelloWorld/HelloWorld.csproj |
| 60 | + ``` |
| 61 | + |
| 62 | +This command creates a new .NET solution named `WebExpress.Tutorial.HelloWorld` and uses .NET 8.0 as the framework. |
| 63 | +- Check the newly created solution. There should now be a new directory named `WebExpress.Tutorial.HelloWorld` in the current directory. You can view the contents of this directory with the command ls (Linux/Mac) or dir (Windows). |
| 64 | +- Open the solution in your preferred development environment. |
| 65 | + - If you are using Visual Studio Code, you can open the solution with the command `code .` in the solution directory. |
| 66 | + |
| 67 | +Now you have created a new solution and are ready to proceed with the next steps in your tutorial. |
| 68 | + |
| 69 | +## Customize the project |
| 70 | +- Add the necessary dependencies in the `HelloWorld` project file. |
| 71 | + ```xml |
| 72 | + <PropertyGroup> |
| 73 | + ... |
| 74 | + <EnableDynamicLoading>true</EnableDynamicLoading> |
| 75 | + </PropertyGroup> |
| 76 | + |
| 77 | + <ItemGroup> |
| 78 | + <PackageReference Include="WebExpress.WebCore" Version="0.0.7-alpha"> |
| 79 | + <Private>false</Private> |
| 80 | + <ExcludeAssets>runtime</ExcludeAssets> |
| 81 | + </PackageReference> |
| 82 | + |
| 83 | + <PackageReference Include="WebExpress.WebUI" Version="0.0.7-alpha"> |
| 84 | + <Private>false</Private> |
| 85 | + <ExcludeAssets>runtime</ExcludeAssets> |
| 86 | + </PackageReference> |
| 87 | + </ItemGroup> |
| 88 | + ``` |
| 89 | +- Add the necessary dependencies in the `HelloWorld.App` project file. |
| 90 | + ```xml |
| 91 | + <ItemGroup> |
| 92 | + <PackageReference Include="WebExpress.WebCore" Version="0.0.7-alpha" /> |
| 93 | + <PackageReference Include="WebExpress.WebUI" Version="0.0.7-alpha" /> |
| 94 | + </ItemGroup> |
| 95 | + |
| 96 | + <ItemGroup> |
| 97 | + <ProjectReference Include="..\HelloWorld\HelloWorld.csproj" /> |
| 98 | + </ItemGroup> |
| 99 | + ``` |
| 100 | +- Adjust the project file to your requirements. |
| 101 | + |
| 102 | +## Configure the WebExpress package |
| 103 | +- Add a file named `WebExpress.Tutorial.HelloWorld.spec` in the solution folder. |
| 104 | + ```xml |
| 105 | + <?xml version="1.0" encoding="utf-8"?> |
| 106 | + <package> |
| 107 | + <id>WebExpress.Tutorial.HelloWorld</id> |
| 108 | + <version>0.0.7-alpha</version> |
| 109 | + <title>HelloWorld</title> |
| 110 | + <authors>rene_schwarzer@hotmail.de</authors> |
| 111 | + <license>MIT</license> |
| 112 | + <icon>icon.png</icon> |
| 113 | + <readme>README.md</readme> |
| 114 | + <privacypolicy>PrivacyPolicy.md</privacypolicy> |
| 115 | + <description>Provides a simple web application that issues a hello world greeting.</description> |
| 116 | + <tags>webexpress tutorial</tags> |
| 117 | + <plugin>HelloWorld</plugin> |
| 118 | + </package> |
| 119 | + ``` |
| 120 | +- Adjust the spec file to your requirements. |
| 121 | +- Add the spec file in the `HelloWorld.App` project file. |
| 122 | + ``` |
| 123 | + <Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="'$(Configuration)' == 'Release'"> |
| 124 | + <Exec Command="$(SolutionDir)$(AssemblyName)/bin/$(Configuration)/$(TargetFramework)/$(AssemblyName).exe -s $(SolutionDir)/WebExpress.Tutorial.HelloWorld.spec -c $(Configuration) -t $(TargetFramework) -o $(SolutionDir)/pkg/$(Configuration)" /> |
| 125 | + </Target> |
| 126 | + ``` |
| 127 | + |
| 128 | +## Create a WebExpress plugin |
| 129 | +- Create a new class `Plugin` in the `HelloWorld` project that implements the `IPlugin` interface. |
| 130 | + ```csharp |
| 131 | + using WebExpress.WebCore.WebAttribute; |
| 132 | + using WebExpress.WebCore.WebPlugin; |
| 133 | + |
| 134 | + namespace HelloWorld |
| 135 | + { |
| 136 | + [Name("HelloWorld:plugin.name")] |
| 137 | + [Description("HelloWorld:plugin.description")] |
| 138 | + [Icon("/assets/img/helloworld.svg")] |
| 139 | + [Dependency("webexpress.webui")] |
| 140 | + public sealed class Plugin : IPlugin |
| 141 | + { |
| 142 | + public void Initialization(IPluginContext context) {} |
| 143 | + public void Run() {} |
| 144 | + public void Dispose() {} |
| 145 | + } |
| 146 | + } |
| 147 | + ``` |
| 148 | +- Adjust the class to your requirements. |
| 149 | + |
| 150 | +## Create a WebApplication |
| 151 | +- Create a new class `Application` in the `HelloWorld` project that implements the `IApplication` interface. |
| 152 | + ```csharp |
| 153 | + using WebExpress.WebCore.WebApplication; |
| 154 | + using WebExpress.WebCore.WebAttribute; |
| 155 | + |
| 156 | + namespace HelloWorld |
| 157 | + { |
| 158 | + [Name("HelloWorld:app.name")] |
| 159 | + [Description("HelloWorld:app.description")] |
| 160 | + [Icon("/assets/img/helloworld.svg")] |
| 161 | + [AssetPath("/")] |
| 162 | + [ContextPath("/helloworld")] |
| 163 | + public sealed class Application : IApplication |
| 164 | + { |
| 165 | + public void Initialization(IApplicationContext context) {} |
| 166 | + public void Run() {} |
| 167 | + public void Dispose() {} |
| 168 | + } |
| 169 | + } |
| 170 | + ``` |
| 171 | +- Adjust the class to your requirements. |
| 172 | + |
| 173 | +## Create a module |
| 174 | +- Create a new class `Module` in the `HelloWorld` project that implements the `IModule` interface. |
| 175 | + ```csharp |
| 176 | + using WebExpress.WebCore.WebAttribute; |
| 177 | + using WebExpress.WebCore.WebModule; |
| 178 | + |
| 179 | + namespace HelloWorld |
| 180 | + { |
| 181 | + [Name("HelloWorld:module.name")] |
| 182 | + [Description("HelloWorld:module.description")] |
| 183 | + [Icon("/assets/img/helloworld.svg")] |
| 184 | + [AssetPath("/")] |
| 185 | + [ContextPath("/")] |
| 186 | + [Application<Application>] |
| 187 | + public sealed class Module : IModule |
| 188 | + { |
| 189 | + public void Initialization(IModuleContext context) {} |
| 190 | + public void Run() {} |
| 191 | + public void Dispose() {} |
| 192 | + } |
| 193 | + } |
| 194 | + ``` |
| 195 | +- Adjust the class to your requirements. |
| 196 | + |
| 197 | +## Create a homepage |
| 198 | +- Create a new view for the homepage in the `HelloWorld` project. |
| 199 | + ```csharp |
| 200 | + using WebExpress.WebCore.Internationalization; |
| 201 | + using WebExpress.WebCore.WebAttribute; |
| 202 | + using WebExpress.WebCore.WebHtml; |
| 203 | + using WebExpress.WebCore.WebPage; |
| 204 | + using WebExpress.WebCore.WebResource; |
| 205 | + using WebExpress.WebCore.WebScope; |
| 206 | + using WebExpress.WebUI.WebControl; |
| 207 | + using WebExpress.WebUI.WebPage; |
| 208 | + |
| 209 | + namespace HelloWorld.WebPage |
| 210 | + { |
| 211 | + [Title("HelloWorld:homepage.label")] |
| 212 | + [Segment(null, "HelloWorld:homepage.label")] |
| 213 | + [ContextPath(null)] |
| 214 | + [Module<Module>] |
| 215 | + public sealed class HomePage : Page<RenderContextControl>, IScope |
| 216 | + { |
| 217 | + public override void Initialization(IResourceContext context) |
| 218 | + { |
| 219 | + base.Initialization(context); |
| 220 | + } |
| 221 | + |
| 222 | + public override void Process(RenderContextControl context) |
| 223 | + { |
| 224 | + context.VisualTree.Favicons.Add(new Favicon(context.Page.ApplicationContext.ContextPath.Append("/assets/img/favicon.png"))); |
| 225 | + context.VisualTree.Content.Add(new ControlText() { Text = InternationalizationManager.I18N("HelloWorld:homepage.text") }); |
| 226 | + } |
| 227 | + } |
| 228 | + } |
| 229 | + ``` |
| 230 | +- Adjust the homepage to your requirements. |
| 231 | + |
| 232 | +## Change the program class |
| 233 | +- Change the program class of the `HelloWorld.App` project as follows: |
| 234 | + ```csharp |
| 235 | + using System.Reflection; |
| 236 | + |
| 237 | + namespace HalloWorld.App |
| 238 | + { |
| 239 | + internal class Program |
| 240 | + { |
| 241 | + static void Main(string[] args) |
| 242 | + { |
| 243 | + var app = new WebExpress.WebCore.WebEx() |
| 244 | + { |
| 245 | + Name = Assembly.GetExecutingAssembly().GetName().Name |
| 246 | + }; |
| 247 | + |
| 248 | + app.Execution(args); |
| 249 | + } |
| 250 | + } |
| 251 | + } |
| 252 | + ``` |
| 253 | + |
| 254 | +## Internationalization |
| 255 | +- Add support for multiple languages. This can be achieved by using i18n files. Each resource file contains the translations for all strings in your application. Name your resource files according to the culture they represent. For example, the file for German translations should be called `de``. In the following, we use the english language. |
| 256 | + ``` |
| 257 | + plugin.name=HelloWorld |
| 258 | + plugin.description= |
| 259 | +
|
| 260 | + app.name=HelloWorld |
| 261 | + app.label=Hello World |
| 262 | + app.description=Tutorial of a simple hello world WebExpress application. |
| 263 | + app.license.label=License MIT |
| 264 | + app.license.uri=https://github.com/ReneSchwarzer/WebExpress.Tutorial.HelloWorld/blob/main/LICENSE |
| 265 | + app.version.label={0} version {1} created with {2} version {3}. |
| 266 | +
|
| 267 | + module.name=HelloWorld |
| 268 | + module.description=Module of the HelloWorld application. |
| 269 | +
|
| 270 | + homepage.label=Home page |
| 271 | + homepage.text=Hello world! |
| 272 | + ``` |
| 273 | + |
| 274 | + - Add the en file in the `HelloWorld` project file. |
| 275 | + ```xml |
| 276 | + <ItemGroup> |
| 277 | + <EmbeddedResource Include="Internationalization/en" /> |
| 278 | + </ItemGroup> |
| 279 | + ``` |
| 280 | + |
| 281 | +## Add the Assets |
| 282 | +- Add assets to the `HelloWorld` project. You can get the assets for this tutorial here: https://github.com/ReneSchwarzer/WebExpress.Tutorial.HelloWorld/tree/main/src/HelloWorld/Assets/img |
| 283 | +- Add the assets in the `HelloWorld` project file. |
| 284 | + ```xml |
| 285 | + <ItemGroup> |
| 286 | + <EmbeddedResource Include="Assets/img/favicon.png" /> |
| 287 | + <EmbeddedResource Include="Assets/img/helloworld.svg" /> |
| 288 | + </ItemGroup> |
| 289 | + ``` |
| 290 | + |
| 291 | +## Add a Configuration |
| 292 | +- The application must be configured. A standard configuration must be delivered for this purpose. Add the configuration file to the `HelloWorld.App` project. |
| 293 | + ```xml |
| 294 | + <?xml version="1.0" encoding="utf-8" ?> |
| 295 | + <config version = "1"> |
| 296 | + <log modus="Off" debug="false" path="/var/log/" encoding="utf-8" filename="inventory.log" timepattern="dd.MM.yyyy HH:mm:ss" /> |
| 297 | + <uri>http://localhost/</uri> |
| 298 | + <endpoint uri="http://localhost/"/> |
| 299 | + <limit> |
| 300 | + <connectionlimit>300</connectionlimit> |
| 301 | + <uploadlimit>3000000000</uploadlimit> |
| 302 | + </limit> |
| 303 | + <culture>en</culture> |
| 304 | + <packages>./packages</packages> |
| 305 | + <assets>./assets</assets> |
| 306 | + <data>./data</data> |
| 307 | + <contextpath></contextpath> |
| 308 | + </config> |
| 309 | + ``` |
| 310 | +- Include the configuration file in the `HelloWorld.App` project file. |
| 311 | + ``` |
| 312 | + <ItemGroup> |
| 313 | + <None Update="config\webexpress.config.xml"> |
| 314 | + <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> |
| 315 | + </None> |
| 316 | + </ItemGroup> |
| 317 | + ``` |
| 318 | + |
| 319 | +## Compile and register in WebExpress |
| 320 | +- Compile the solution as a release. To do this, open the command line or terminal in the solution directory and run the following command: |
| 321 | + ```bash |
| 322 | + dotnet build --configuration Release |
| 323 | + ``` |
| 324 | + This command compiles the solution in release mode. You can find the compiled files in the `bin/Release` directory of your project. |
| 325 | + |
| 326 | +- Run the solution by starting the `HelloWorld.App` project. |
| 327 | + ```bash |
| 328 | + cd HelloWorld.App\bin\bin\Release\net8.0 |
| 329 | + dotnet run --project ../../../HelloWorld.App.csproj |
| 330 | + ``` |
| 331 | + |
| 332 | +- After compiling, there should be a file with the `.wxp` extension in the `pkg/Release` directory. This file do you need in WebExpress. |
| 333 | + |
| 334 | +Good luck! |
| 335 | + |
| 336 | +# Tags |
| 337 | +#WebExpress #WebServer #WebCore #WebUI #Tutorial #DotNet |
0 commit comments