Blazor 5x - قسمت 24 - تهیه API مخصوص Blazor WASM - بخش 1 - ایجاد تنظیمات ابتدایی
نویسنده: وحید نصیری
تاریخ: ۱۴۰۰/۰۱/۰۱ ۱۴:۲۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
namespace BlazorWasm.WebApi
{
public class Startup
{
//...
public void ConfigureServices(IServiceCollection services)
{
services.AddAutoMapper(typeof(MappingProfile).Assembly);
services.AddScoped<IHotelRoomService, HotelRoomService>();
services.AddScoped<IAmenityService, AmenityService>();
services.AddScoped<IHotelRoomImageService, HotelRoomImageService>();
var connectionString = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connectionString));
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
//... <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\BlazorServer\BlazorServer.DataAccess\BlazorServer.DataAccess.csproj" />
<ProjectReference Include="..\..\BlazorServer\BlazorServer.Services\BlazorServer.Services.csproj" />
<ProjectReference Include="..\..\BlazorServer\BlazorServer.Models.Mappings\BlazorServer.Models.Mappings.csproj" />
</ItemGroup>
</Project> {
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=HotelManagement;Trusted_Connection=True;MultipleActiveResultSets=true"
}
} using BlazorServer.Models;
using BlazorServer.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace BlazorWasm.WebApi.Controllers
{
[Route("api/[controller]")]
public class HotelRoomController : ControllerBase
{
private readonly IHotelRoomService _hotelRoomService;
public HotelRoomController(IHotelRoomService hotelRoomService)
{
_hotelRoomService = hotelRoomService;
}
[HttpGet]
public IAsyncEnumerable<HotelRoomDTO> GetHotelRooms()
{
return _hotelRoomService.GetAllHotelRoomsAsync();
}
[HttpGet("{roomId}")]
public async Task<IActionResult> GetHotelRoom(int? roomId)
{
if (roomId == null)
{
return BadRequest(new ErrorModel
{
Title = "",
ErrorMessage = "Invalid Room Id",
StatusCode = StatusCodes.Status400BadRequest
});
}
var roomDetails = await _hotelRoomService.GetHotelRoomAsync(roomId.Value);
if (roomDetails == null)
{
return BadRequest(new ErrorModel
{
Title = "",
ErrorMessage = "Invalid Room Id",
StatusCode = StatusCodes.Status404NotFound
});
}
return Ok(roomDetails);
}
}
} namespace BlazorServer.Models
{
public class ErrorModel
{
public string Title { get; set; }
public int StatusCode { get; set; }
public string ErrorMessage { get; set; }
}
}
namespace BlazorWasm.WebApi
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddCors(o => o.AddPolicy("HotelManagement", builder =>
{
builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
}));
services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = null;
// To avoid `JsonSerializationException: Self referencing loop detected error`
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
});
// ...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// ...
app.UseCors("HotelManagement");
app.UseRouting();
app.UseAuthentication();
// ...
}
}
} var secretUrl = "api/WeatherForecast/_secretUrl";
var secretUrl = ApiUrls.WeatherForecast.HttpGet.SecretUrl;