Webinar Asp.Net 5
Selamlar;
Öncelikle webinar’a katılan tüm arkadaşlara teşekkür ederim. Umarım sizin için faydalı olmuştur. Kaçıranlar için webinar kaydı ve kodları aşağıdadır.
Asp.Net 50 ve Vnext ile gelen yenilikleri , güzel bir projeyi kodlayarak inceledik.
Konular:
- Kendimi tanıtım ve Asp.Net5 boş bir web form oluşturma. Ve yeni gelen Ctrl + F5 derleme özellikleri.
- Startup.cs ve Project.json tanımlama ve örnekler.
- Mvc routing işlemleri ve Controller yapısı.
- CLR tipleri ASP.NET.50 ve ASP.NET Core 5.0 tanımı ve örnekleri.
- Model tasarlama ve View Mantığı. İlgili modelin Asp.Net.50 ile injection yapılması.
- WebApi Services yaratıp ilgili modeli overwrite etme. Aynı project içindeki İlgili services’i Action’da çağırma.
- Config.json yaratıp webapi servicesinden ilgili message’ı okuma.
- NodeJS ve console örnekleri.
- Self Hosting Project.json ayarları. Kvm ve consoledan mesaj mvc web forma parametre gönderme.
- Sorular, cevaplar ve kapanış.
Kaynaklar:
- K. Scott Allen video ve yazıları.
- http://github.com/aspnet/home
- http://www.asp.net/vnext
- https://channel9.msdn.com/
Webinar Kaydı:
Startup.cs: IConfiguration interface ile config.json dosyası tanımlanmıştır. Mvc tanımlaması ve routing isşlemleri Configure() methodu altında yapılmış. Yaratılan ChatMessage model’inin injection işlemi ve Mvc serviceslerinin eklenmesi ConfigureServices() altında yapılmıştır.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using System; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Http; using Microsoft.Framework.DependencyInjection; using AspNet5Webinar.Services; using Microsoft.Framework.ConfigurationModel; namespace AspNet5Webinar { public class Startup { public IConfiguration Configuration { get; set; } public Startup() { Configuration = new Configuration() .AddJsonFile("config.json") .AddEnvironmentVariables(); } // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddInstance<IChatMessage>(new ChatServices(Configuration)); } public void Configure(IApplicationBuilder app) { app.UseMvc(); //app.UseMvc(routes=>routes.MapRoute("defualt", "{controller=Home}/{action=ShowGallery}")); //app.Run(async re => await re.Response.WriteAsync("Wellcome AspNet5 Webinarine Hosgeldiniz!!")); //app.UseWelcomePage(); } } } |
Project.json:
- “dependencies”: Ilgili Mvc: Mvc projesi oluşturmak için, Diagnostics: WellComePage yaratmak için, Configuration:config.json’dan veri okumak için ve Weblistener: SelfHosting yapmak için paketleri nuget gibi burada tanımlanıp download edilir.
- frameworks: aspnet50 veya aspnetcore50 clr’dan hangisine ilgili paket indirlmek istenir ise burada tanımlanır.
- commands: Self hosting yani virtual IIS gerekmeden yayınlama yapabilmek için belirtilen komu ile mesela burda “Web” ilgili server.urls hhtp://localhost:1453′in tanımlandığı yerdir.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
{ "webroot": "wwwroot", "version": "1.0.0-*", "dependencies": { "Microsoft.AspNet.Server.IIS": "1.0.0-beta3", "Microsoft.AspNet.Diagnostics": "1.0.0-beta3", "Microsoft.AspNet.Mvc": "6.0.0-beta3", "Microsoft.Framework.ConfigurationModel.Json": "1.0.0-beta3", "Microsoft.AspNet.Server.WebListener": "1.0.0-beta3" }, "frameworks": { "aspnet50": { }, "aspnetcore50": { } }, "bundleExclude": [ "node_modules", "bower_components", "**.kproj", "**.user", "**.vspscc" ], "exclude": [ "wwwroot", "node_modules", "bower_components" ], "commands": { "Web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:1453" } } |
ChatMessage: View’a gönderilecek tanımlanan modeldir. ChatServices class’ının Construactor’ında IConfiguration ile config.json alıp, gelen model’in Text’ini overwrite etmektedir.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using System; using Microsoft.Framework.ConfigurationModel; namespace AspNet5Webinar.Services { public class ChatMessage { public int ID { get; set; } public string Text { get; set; } } public interface IChatMessage { ChatMessage GetToDayMessage(); } public class ChatServices : IChatMessage { IConfiguration _config; public ChatServices(IConfiguration config) { _config = config; } int _id = 1; public ChatMessage GetToDayMessage() { return new ChatMessage { ID = _id++, //Text = "AspNet5'den Selamlar_" + _id Text = _config.Get("message") }; } } } |
Config.json: message şeklinde bir parametre tanımlanmıştır. Bu connection strings veya azure cloude services tanımlamaları da olabilir.
1 2 3 |
{ "message": "Bu Mesaj Config.Json'dan Gönderilmiştir." } |
HomeController.cs: Routing işlemleri [Route(“[controller]”),Route(“/”)] şeklinde de yapılabilmektedir. Ayrıca ASPNET50 VE ASPNETCORE50 için ayrı ayrı derleme nasıl yapılacağı aşağıdaki gibi kodlanmıştır. Constructor’ında yazılmış model olan IChatMessage interfaceini beklemektedir.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
using Microsoft.AspNet.Mvc; using AspNet5Webinar.Services; using AspNet5Webinar.Controllers.Controllers; // For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860 namespace AspNet5Webinar.Controllers { [Route("[controller]"), Route("/")] public class HomeController : Controller { // GET: /<controller>/ IChatMessage _message; public HomeController(IChatMessage message) { _message = message; } [Route("[action]"), Route("")] public IActionResult ShowGallery() { /* string message = string.Empty; #if ASPNET50 message = Assembly.GetExecutingAssembly().FullName; #endif #if ASPNETCORE50 message = typeof(HomeController).AssemblyQualifiedName; #endif return Content(message); */ //return Content("AspNet5 Mvc First Controller Message"); //var model = _message.GetToDayMessage(); var model = new Chat(_message).Get(_message.GetToDayMessage()); return View(model); } } } |
Chat.cs: Webapi servicesidir. HomeController ile aynı proje içinde tanımlanmıştır. Get() methodu değiştirilerek ChatMessage modelini bekleyen ve model’in Text’i overwrite edilir.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
using System; using System.Collections.Generic; using System.Linq; using Microsoft.AspNet.Mvc; using AspNet5Webinar.Services; // For more information on enabling Web API for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860 namespace AspNet5Webinar.Controllers.Controllers { [Route("api/[controller]")] public class Chat : Controller { IChatMessage _chat; public Chat(IChatMessage chat) { _chat = chat; } [HttpGet] public ChatMessage Get(ChatMessage mod) { var model = mod; model.Text = "WebApi Services'den Gönderilmiştir. " + mod.Text; return model; } //public ChatMessage Get(string deger) //{ // var model = _chat.GetToDayMessage(); // model.Text = "WebApi Services'den Gönderilmiştir. " + deger; // return model; //} // POST api/values [HttpPost] public void Post([FromBody]string value) { } // PUT api/values/5 [HttpPut("{id}")] public void Put(int id, [FromBody]string value) { } // DELETE api/values/5 [HttpDelete("{id}")] public void Delete(int id) { } } } |
Yeni bir webinarda görüşmek üzere hepinize hoşçakalın.
Source Code: http://www.borakasmer.com/projects/AspNet5Webinar.rar
Hocam Selamlar;
Muhteşem bir webinarmış. Kaçırmışım ama şimdi izledim. Elinize kolunuza sağlık. Bir de geçmiş olsun hocam:( Acil şifalar.
İyi tatiller.