From 38b70b545d190b2440b9eef96b27fb2b0ff3e851 Mon Sep 17 00:00:00 2001 From: fausio luis Date: Sat, 4 Jan 2020 22:17:02 +0200 Subject: [PATCH] Implementing app.UseMvc(configureRoutes); costumer routes --- Helloword_core/Controllers/HomeController.cs | 9 +++---- .../Controllers/ProductController.cs | 25 +++++++++++++++++++ Helloword_core/Controllers/UserController.cs | 20 +++++++++++++++ Helloword_core/Startup.cs | 18 ++++++++++--- 4 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 Helloword_core/Controllers/ProductController.cs create mode 100644 Helloword_core/Controllers/UserController.cs diff --git a/Helloword_core/Controllers/HomeController.cs b/Helloword_core/Controllers/HomeController.cs index 075e174..8f112d1 100644 --- a/Helloword_core/Controllers/HomeController.cs +++ b/Helloword_core/Controllers/HomeController.cs @@ -8,18 +8,15 @@ namespace Helloword_core.Controllers { - [Route("Home")] + public class HomeController : Controller - { - [Route("")] - [Route("Index")] + { // GET: // public string Index() { return "Hello from HomeController"; } - - [Route("About")] + public string About() { return "About"; diff --git a/Helloword_core/Controllers/ProductController.cs b/Helloword_core/Controllers/ProductController.cs new file mode 100644 index 0000000..6801c67 --- /dev/null +++ b/Helloword_core/Controllers/ProductController.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; + +// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 + +namespace Helloword_core.Controllers +{ + + [Route("[Controller]/[Action]")] + public class ProductController : Controller + { + // GET: // + public List Index() + { + return new List() + { + "Item 1", + "Item 2" + }; + } + } +} diff --git a/Helloword_core/Controllers/UserController.cs b/Helloword_core/Controllers/UserController.cs new file mode 100644 index 0000000..7bc47c4 --- /dev/null +++ b/Helloword_core/Controllers/UserController.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; + +// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 + +namespace Helloword_core.Controllers +{ + + public class UserController : Controller + { + // GET: // + public string Index() + { + return "from User Index"; + } + } +} diff --git a/Helloword_core/Startup.cs b/Helloword_core/Startup.cs index 13fb791..f068b5f 100644 --- a/Helloword_core/Startup.cs +++ b/Helloword_core/Startup.cs @@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -29,7 +30,8 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILogger< } app.UseStaticFiles(); - app.UseMvcWithDefaultRoute(); + app.UseMvc(configureRoutes); + app.Map("/test", testPipeline); app.Use(next => async context => { @@ -39,11 +41,11 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILogger< app.Run(async (context) => { - logger.LogInformation("Response Served."); + logger.LogInformation("Response Served."); await context.Response.WriteAsync(hello.SayHello()); }); } - + #region testPipelines private void testPipeline(IApplicationBuilder app) { app.MapWhen(context => { return context.Request.Query.ContainsKey("ln"); }, testPipeline1); @@ -71,5 +73,15 @@ private void testPipeline2(IApplicationBuilder app) await context.Response.WriteAsync("hello from teste testPipeline2 kay in;"); }); } + #endregion + + + private static void configureRoutes(IRouteBuilder routes) + { + routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}"); + routes.MapRoute("admin", "admin/{controller=User}/{action=Index}/{id?}"); + routes.MapRoute("shop", "{controller=Product}/{action=Index}/{id?}"); + + } } }