Mavzu: Web dasturlash Reja



Yüklə 0,79 Mb.
səhifə6/6
tarix05.04.2022
ölçüsü0,79 Mb.
#54777
1   2   3   4   5   6
Mavzu Web dasturlash Reja

new Book() {

Title = "Pride and Prejudice",

Year = 1813,

Author = austen,

Price = 9.99M,

Genre = "Comedy of manners"

},

new Book() {

Title = "Northanger Abbey",

Year = 1817,

Author = austen,

Price = 12.95M,

Genre = "Gothic parody"

},

new Book() {

Title = "David Copperfield",

Year = 1850,

Author = dickens,

Price = 15,

Genre = "Bildungsroman"

},

new Book() {

Title = "Don Quixote",

Year = 1617,

Author = cervantes,

Price = 8.95M,

Genre = "Picaresque"

}

);

context.SaveChanges();



}

}

}



}
Siz ushbu ma'lumot sinfini ishlab chiqarish kodiga kiritmaysiz, ammo u bizning stsenariyimiz uchun ishlaydi.

Keyingi, Solution Explorer-da, Startup.cs faylini oching. Configure usuliga quyidagi satrlarni qo'shing:



SampleData.Initialize(app.ApplicationServices);

Shundan so'ng, to'liq Startup.cs fayli quyidagicha bo'ladi:



using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

using Microsoft.AspNet.Builder;

using Microsoft.AspNet.Hosting;

using Microsoft.AspNet.Identity.EntityFramework;

using Microsoft.Data.Entity;

using Microsoft.Extensions.Configuration;

using Microsoft.Extensions.DependencyInjection;

using Microsoft.Extensions.Logging;

using ContosoBooks.Models;

using ContosoBooks.Services;

namespace ContosoBooks

{

public class Startup

{

public Startup(IHostingEnvironment env)

{

// Set up configuration sources.

var builder = new ConfigurationBuilder()

.AddJsonFile("appsettings.json")

.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

if (env.IsDevelopment())

{

// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709

builder.AddUserSecrets();

}

builder.AddEnvironmentVariables();

Configuration = builder.Build();

}

public IConfigurationRoot Configuration { get; set; }

// This method gets called by the runtime. Use this method to add services to the container.

public void ConfigureServices(IServiceCollection services)

{

// Add framework services.

services.AddEntityFramework()

.AddSqlServer()

.AddDbContext(options =>

options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

services.AddIdentity()

.AddEntityFrameworkStores()

.AddDefaultTokenProviders();

services.AddMvc();

// Add application services.

services.AddTransient();

services.AddTransient();

}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)

{

loggerFactory.AddConsole(Configuration.GetSection("Logging"));

loggerFactory.AddDebug();

if (env.IsDevelopment())

{

app.UseBrowserLink();

app.UseDeveloperExceptionPage();

app.UseDatabaseErrorPage();

}

else

{

app.UseExceptionHandler("/Home/Error");

// For more details on creating database during deployment see http://go.microsoft.com/fwlink/?LinkID=615859

try

{

using (var serviceScope = app.ApplicationServices.GetRequiredService()

.CreateScope())

{

serviceScope.ServiceProvider.GetService()

.Database.Migrate();

}

}

catch { }

}

app.UseIISPlatformHandler(options => options.AuthenticationDescriptions.Clear());

app.UseStaticFiles();

app.UseIdentity();

// To configure external authentication please see http://go.microsoft.com/fwlink/?LinkID=532715

app.UseMvc(routes =>

{

routes.MapRoute(

name: "default",

template: "{controller=Home}/{action=Index}/{id?}");

});

SampleData.Initialize(app.ApplicationServices);

}

// Entry point for the application.

public static void Main(string[] args) => WebApplication.Run(args);

}

}

E'tibor bering, ConfigureServices-da dastur Configuration["Data:DefaultConnection:ConnectionString"] -ga ma'lumotlar bazasiga ulanish simini olish uchun qo'ng'iroq qiladi. Dasturlash paytida ushbu sozlama appsettings.json faylidan kelib chiqadi. Ilovani ishlab chiqarish muhitida joylashtirganingizda, ulanish chizig'ini xostdagi atrof-muhit o'zgaruvchisiga o'rnatasiz. Agar Configuration API xuddi shu kalit bilan muhit o'zgaruvchisini topsa, u appsettings.json-da topilgan qiymat o'rniga atrof-muhit o'zgaruvchisini qaytaradi.

Veb-ilovalarni yaratish

Barcha sinflar va o'zgarishlarning ishlashiga ishonch hosil qilish uchun siz dastur yaratishingiz kerak.

"Build" menyusidan "Build Solution" -ni tanlang.

Output oynasi ko'rsatiladi va agar hamma narsa yaxshi bo'lsa, siz xabar olasiz.



Agar xato yuzaga kelsa, avvalgi bosqichlarni ko'rib chiqing. Output oynasidagi ma'lumotlar muammoning qaysi faylida paydo bo'lganligi va o'zgartirish talab qilinadigan joyni ko'rsatadi. Ushbu ma'lumot muammoni hal qilish uchun avvalgi qaysi amallarni takrorlash kerakligini aniqlashga yordam beradi.

Izoh Ilovani ishga tushirishdan oldin ma'lumotlar ko'chirishidan foydalanib ma'lumotlar bazasini yaratishingiz kerak.

Ma'lumotlar bazasini yaratish uchun ma'lumotlar migratsiyasidan foydalanish

EF ma'lumotlarning ko'chishi butun dastur davomida modellarni yangilash uchun ishlatiladi. Dastlab ma'lumotlar bazasini yaratish uchun ma'lumotlar migratsiyasidan foydalangan holda, model o'zgartirilgandan so'ng ma'lumotlar bazasini osongina o'zgartirishingiz mumkin. Bu sizga dasturni yanada samarali yaratish va saqlashga imkon beradi. Ma'lumotlar bazasini o'zgartirishdan keyin modelga yoki sxemaga o'zgartirishlar kiritish kerak bo'lganda ma'lumotlar ko'chirilishiga alternativa butun ma'lumotlar bazasini qayta yaratishdir.

Loyiha direktoriyasida Command Prompt-ni oching (ContosoBooks / src / ContosoBooks).

Izoh Buyruqning so'rovini ochish uchun Windows-da boshlash tugmachasini o'ng tugmasini bosib, menyudan CommandPrompt-ni tanlang.

Loyihalar katalogini topish uchun Solution Explorer-da Visual Studio-da loyiha nomini (ContosoBooks) sichqonchaning o'ng tugmasi bilan bosing va File Explorer-da Ochiq papkani tanlang. Loyiha yo'lini File Explorerdan Command Prompt-ga nusxalash. Masalan, katalogni o'zgartirish uchun buyruq so'rovidan quyidagilarni kiriting:

cd C:\Projects\ContosoBooks\src\ContosoBooks

Izoh Src papkasidagi ContosoBooks papkasiga borganingizga ishonch hosil qiling.

Buyruq satridan quyidagi buyruqlarni bajaring:

dnu restore

dnx ef migrations add Initial

dnx ef database update

Izoh Agar dnu restore tan olinmasa, bosqichni dastlabki tafsilotlar bilan (yoki qisman) to'liq takrorlashingiz kerak. Birinchidan, .NET Version Manager (dnvm) ning Active versiyasini tekshirishingiz kerak. Buning uchun buyruq satriga dnvmlist kiriting. Agar biron-bir versiyaning yonida * bo'lmasa, faol versiyani o'rnating, ya'ni kerakli versiya tanlanishi uchun dnvm use 1.0.0-rc1-update1 -p kiriting.

.NET Version Manager (dnvm) bu .NET Runtime-ni yangilash va sozlash uchun ishlatiladigan buyruqlar qatori yordam dasturlari to'plami.

DNX .NET Expression Environment degan ma'noni anglatadi. Ef buyrug'i loyihaning project.json faylida ko'rsatilgan. Dnvm, dnu va dnx haqida qo'shimcha ma'lumotni bu erdan olishingiz mumkin: DNX Overview.

AddInitial buyrug'i Initial migratsiyani yaratadi, bu esa loyihaga kod qo'shib, EFga ma'lumotlar bazasi sxemasini yangilashga imkon beradi. update buyrug'i zamonaviy ma'lumotlar bazasini yaratadi. Ushbu buyruqni bajargandan so'ng, Migrations papkasi yangilanadi:



Izoh Agar sizga EF buyruqlari bilan yordam kerak bo'lsa, buyruq satriga quyidagilarni kiriting: dnx ef -?. Qo'shish buyrug'iga yordam berish uchun quyidagilarni kiriting: dnx efmigrations add -?. Va update buyrug'iga yordam berish uchun quyidagilar: dnx ef database update -?.

Bundan tashqari, SQL Server Object Explorer-da yangi ma'lumotlar bazasini ko'rishingiz mumkin.

Navigatsiya qo`shish.

Veb-dastur uchun navigatsiyani yangilang. Solution Explorer-dan "Views / Shared / _Layout.cshtml" faylini oching. Quyidagi kodni toping:

  • Home


  • About


  • Contact
  • Yuqoridagi kodni shu bilan almashtiring:



  • Books


  • Authors
  • Ushbu o'zgartirishlar Books ko'rinishiga va Authors ko'rinishiga havolani qo'shadi. Siz o'zingizning loyihangizga iskala qo'shib qo'yganingizda ushbu qarashlarni yaratgansiz.

    Veb-dastur yaratish. Har bir narsa ishlayotganiga ishonch hosil qilish uchun siz dasturni yana yaratishingiz kerak. "Build" menyusidan "Build Solution" -ni tanlang.

    Veb-ilovani lokal ravishda ishga tushirish.

    Butun mahsulot yoki mahsulot toifasi qanday ko'rinishini ko'rish uchun ilovani ishga tushiring.

    Solution Explorer-da, loyiha nomini o'ng tugmasini bosing va Brauzerda View ->View-ni tanlang. Yoki shunchaki F5 tugmasini bosing.

    Brauzer dasturni ochadi va ko'rsatadi. Sahifaning yuqori qismidagi Book havolasini bosing.

    Brauzerni yoping va Visual Studio-dagi "StopDebugging" belgisini bosing. Ilovani Visual Studio-da Azure In Solution Explorer-da e'lon qilgandan so'ng, sichqonchaning o'ng tugmachasini bosib, Publish-ni tanlang.



    Publish Web oynasida Microsoft Azure Web Apps-ni bosing va Azure-ga kiring.



    Shundan so'ng, Azure-da yangi dastur yaratish uchun New-ni bosing.



    Saytga nom bering, xizmat rejasini, resurslar guruhini va mintaqani tanlang. Ma'lumotlar bazasi serverini, shuningdek, foydalanuvchi nomi va parolni tanlang. Agar siz allaqachon ma'lumotlar bazasi serverini yaratgan bo'lsangiz, undan foydalaning. Shundan so'ng, Create-ni bosing.



    Connection-da Publish Web oynasida Publish-ni bosing.



    Siz nashrning borishini Visual Studio-da yoki Azure App Service Activity oynasida ko'rishingiz mumkin.



    Azure-da nashr etilgandan so'ng, Azure-da ishlaydigan ilovangiz brauzerda ko'rsatiladi.




    Adabiyotlar:

    1. Чамберс Джеймс, Пэкетт Дэвид, Тиммс Саймон. ASP.NET Core. Разработка приложений. Спб.: Питер, 2018. – 464 с.

    2. Сепла Д. Microsoft ADO.NET C# Пер. с англ. М.: Издательско – торговый дом “Русская редакция” . 2003. – 640 с.

    3. https://metanit.com/sharp/mvc5/2.1.php

    Yüklə 0,79 Mb.

    Dostları ilə paylaş:
    1   2   3   4   5   6




    Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
    rəhbərliyinə müraciət

    gir | qeydiyyatdan keç
        Ana səhifə


    yükləyin