Blazor 5x - قسمت 27 - برنامهی Blazor WASM - کار با سرویسهای Web API
نویسنده: وحید نصیری
تاریخ: ۱۴۰۰/۰۱/۰۴ ۱۶:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
namespace BlazorWasm.Client.Services
{
public interface IClientHotelRoomService
{
public Task<IEnumerable<HotelRoomDTO>> GetHotelRoomsAsync(DateTime checkInDate, DateTime checkOutDate);
public Task<HotelRoomDTO> GetHotelRoomDetailsAsync(int roomId, DateTime checkInDate, DateTime checkOutDate);
}
} namespace BlazorWasm.Client.Services
{
public class ClientHotelRoomService : IClientHotelRoomService
{
private readonly HttpClient _httpClient;
public ClientHotelRoomService(HttpClient httpClient)
{
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
}
public Task<HotelRoomDTO> GetHotelRoomDetailsAsync(int roomId, DateTime checkInDate, DateTime checkOutDate)
{
throw new NotImplementedException();
}
public Task<IEnumerable<HotelRoomDTO>> GetHotelRoomsAsync(DateTime checkInDate, DateTime checkOutDate)
{
// How to url-encode query-string parameters properly
var uri = new UriBuilderExt(new Uri(_httpClient.BaseAddress, "/api/hotelroom"))
.AddParameter("checkInDate", $"{checkInDate:yyyy'-'MM'-'dd}")
.AddParameter("checkOutDate", $"{checkOutDate:yyyy'-'MM'-'dd}")
.Uri;
return _httpClient.GetFromJsonAsync<IEnumerable<HotelRoomDTO>>(uri);
}
}
} using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;
using BlazorServer.Models;
using BlazorWasm.Client.Utils;
using System;
using System.Collections.Specialized;
using System.Web;
namespace BlazorWasm.Client.Utils
{
public class UriBuilderExt
{
private readonly NameValueCollection _collection;
private readonly UriBuilder _builder;
public UriBuilderExt(Uri uri)
{
_builder = new UriBuilder(uri);
_collection = HttpUtility.ParseQueryString(string.Empty);
}
public UriBuilderExt AddParameter(string key, string value)
{
_collection.Add(key, value);
return this;
}
public Uri Uri
{
get
{
_builder.Query = _collection.ToString();
return _builder.Uri;
}
}
}
} @using BlazorWasm.Client.Services @using BlazorServer.Models
namespace BlazorWasm.Client
{
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
// ...
builder.Services.AddScoped<IClientHotelRoomService, ClientHotelRoomService>();
// ...
}
}
} namespace BlazorWasm.WebApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class HotelRoomController : ControllerBase
{
// ...
[HttpGet]
public async Task<IActionResult> GetHotelRooms(DateTime? checkInDate, DateTime? checkOutDate)
{
// ...
}
[HttpGet("{roomId}")]
public async Task<IActionResult> GetHotelRoom(int? roomId, DateTime? checkInDate, DateTime? checkOutDate)
{
// ...
}
}
} namespace BlazorServer.Services
{
public interface IHotelRoomService : IDisposable
{
Task<List<HotelRoomDTO>> GetAllHotelRoomsAsync(DateTime? checkInDate, DateTime? checkOutDate);
Task<HotelRoomDTO> GetHotelRoomAsync(int roomId, DateTime? checkInDate, DateTime? checkOutDate);
// ...
}
} {
"BaseAPIUrl": "https://localhost:5001/"
} namespace BlazorWasm.Client
{
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
// ...
// builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddScoped(sp => new HttpClient
{
BaseAddress = new Uri(builder.Configuration.GetValue<string>("BaseAPIUrl"))
});
// ...
}
}
} @page "/hotel/rooms"
@inject ILocalStorageService LocalStorage
@inject IJSRuntime JsRuntime
@inject IClientHotelRoomService HotelRoomService
<h3>HotelRooms</h3>
@code {
HomeVM HomeModel = new HomeVM();
IEnumerable<HotelRoomDTO> Rooms = new List<HotelRoomDTO>();
protected override async Task OnInitializedAsync()
{
try
{
var model = await LocalStorage.GetItemAsync<HomeVM>(ConstantKeys.LocalInitialBooking);
if (model is not null)
{
HomeModel = model;
}
else
{
HomeModel.NoOfNights = 1;
}
await LoadRooms();
}
catch (Exception e)
{
await JsRuntime.ToastrError(e.Message);
}
}
private async Task LoadRooms()
{
Rooms = await HotelRoomService.GetHotelRoomsAsync(HomeModel.StartDate, HomeModel.EndDate);
}
} {
"iisSettings": {
"iisExpress": {
"applicationUrl": "http://localhost:62930",
"sslPort": 44314
}
}
} {
"BlazorWasm.Client": {
"applicationUrl": "https://localhost:5002;http://localhost:5003",
}
} https://localhost:5001;http://localhost:5000
"Client_URL": "https://localhost:5002/"
https://localhost:5002;http://localhost:5003
"BaseAPIUrl": "https://localhost:5001/"
Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException: Unexpected end of request content.
"profiles": {
"Name.Server": {
"hotReloadEnabled": false, <ItemGroup> <Watch Remove="wwwroot\**\*" /> </ItemGroup>