Page 21 - MSDN Magazine, September 2018
P. 21
Displaying the Current Conditions The Razor script in Figure 3 will now be updated to include the current temperature, the high and low, a description of the current conditions, and an icon that corresponds to the current conditions. Notice how the tempera- tures are being passed to the ConvertKtoF function created earlier.
Adding National Weather Service Alerts
The final Web service will retrieve alerts from the National Weather Service (NWS) for the state the ZIP code is in.
Calling the NWS Web Service The following block of code retrieves all the current severe weather alerts from the NWS:
alerts = await Http.GetJsonAsync<Models.Alert>( $"https://api.weather.gov/alerts/active/area/{ziplookup.places[0].stateabbr}");
The state abbreviation retrieved from the ziplookup Web service is used to filter the alerts to the same state.
Deserializing NWS Alerts Data The JSON data needs to be deserialized into a set of classes that aligns with the NWS JSON schema. The code snippet in Figure 4 holds the alerts returned from the NWS Web service.
Displaying the Severe Weather Alerts Now that the alerts have been retrieved, the code in Figure 5 is added to the Razor view to display them. It creates a table to display the alerts and uses @foreach to loop through and display each alert. The effective date, headline, and description are displayed for each alert. If there are no alerts for the state, the table will be empty.
Putting It All Together
The application is now complete, as shown in Figure 6. It runs entirely in the browser and calls three external Web services to display the current weather conditions and alerts.
Publishing the Blazor Application
Publishing a Blazor application is just as easy as publishing any other ASP.NET application. Doing this from Visual Studio will com- pile the application code and generate a complete Web application with all of the final HTML, CSS, scripts and Blazor binaries needed. A good starting place is to use the Publish to Folder option. All of the files of the application get put there. If you look inside the Dist folder, you’ll find a standard index.htm page and an _framework folder. The _framework folder contains all of the compiled assemblies, as well as the Blazor and Mono runtime components.
Next Steps
Blazor is still an experimental framework, though the team is moving forward at a very fast rate of release. The Blazor roadmap details a full framework with routing, components, layouts and more. The Blazor project is being developed in the open on GitHub. Visit the blazor.net page tokeepup-to-dateandstartexperimentingwiththecurrentbits. n
Jonthan Miller is a senior architect. He’s been developing products on the Microsoft stack for a decade and programming on .NET since its inception. Waldman is a full- stack product developer with expertise in front-end technologies (Windows Forms, Windows Presentation Foundation, Silverlight, ASP.NET, AngularJS/Bootstrap), middleware (Windows services, Web API), and back ends (SQL server, Azure).
thanks to the following technical experts who reviewed this article: Dino Esposito (BaxEnergy), Daniel Roth (Microsoft)
msdnmagazine.com
®
Instantly Search Terabytes
dtSearch’s document filters support: • popular file types
• emails with multilevel attachments • a wide variety of databases
• web data
Over 25 search options including:
• efficient multithreaded search •
• forensics options like credit card search
• SDKs for Windows, UWP, Linux, Mac, iOS in beta, Android in beta
• FAQs on faceted search, granular data classification, Azure and more
Visit dtSearch.com for
• hundreds of reviews and case studies
• fully-functional enterprise and developer evaluations
The Smart Choice for Text Retrieval® since 1991
easy
multicolor
hit-
highlighting
Developers:
• APIs for .NET, C++ and Java; ask about new cross-platform .NET Standard SDK with Xamarin and .NET Core
dtSearch.com 1-800-IT-FINDS