html

Parte 5 – Agregar Swagger a GestaProject.WebAPI

Parte 5 – Agregar Swagger a GestaProject.WebAPI

 

 

 

 

 

 

 

Parte​​ 5​​ ​​ Agregar Swagger a GestaProject.WebAPI.

 

En el proyecto de GestaProject.WebAPI, necesitamos agregar algunas librerías y hacer unas pocas configuraciones en el código.

 

Swashbuckle.AspNetCore​​ ver. 4.0.1

Swashbuckle.AspNetCore.Swagger​​ ver. 4.0.1

Swashbuckle.AspNetCore.SwaggerGen​​ ver. 4.0.1

Swashbuckle.AspNetCore.SwaggerUI​​ ver. 4.0.1

 

Parar instalar esos paquetes, damos click derecho en el projecto GestaProject.WebAPI y seleccionamos Management Nuget Packages.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Buscamos los paquetes y los instalamos.

 

Nota: es muy importante que instales la versión 4.0.1

 

Ahora tenemos que editar el archivo Startup.cs del proyecto.​​ 

 

En​​ ConfigureServices, agregamos las siguientes líneas:

 

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2)

                .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());

​​ 

            services.AddSwaggerGen(options =>

            {

                options.SwaggerDoc("v1"new Swashbuckle.AspNetCore.Swagger.Info { Title = "GestaProject Web API", Version = "v1" });

            });

 

 

 

 

 

 

En​​ Configure​​ agregamos las siguientes líneas:

 

app.UseSwagger();

​​ 

            app.UseSwaggerUI(c =>

            {

                c.SwaggerEndpoint("/swagger/v1/swagger.json""GestaProject Web API v1");

            });

 

Quedando algo así:

 

 

 

En​​ ValuesController, haremos un cambio:

 

 

 

 

 

Y por​​ último​​ cambiamos la opción de Lanch browser del proyecto.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Y si todo, les sale bien, ejecutan la solución y verán la siguiente vista.

 

Read More

Parte 2 – Arquitectura de la aplicación web

Parte 2 – Arquitectura de la aplicación web

 

 

 

 

 

 

 

A continuación se muestra de formal general la arquitectura que tendrá la aplicación web.

Arquitectura

 

Read More

Web APP con ASP .NET CORE MVC

Web APP con ASP .NET CORE MVC

Vamos a crear una Web App con las siguientes tecnologías:
Visual Studio 2017
ASP .NET CORE MVC
Microsoft SQL Server
C#
HTML
CSS
JavaScript
JQuery
BootStrap
DevExtreme DataGrid

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });       services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
                .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
            services.AddDbContext<SchoolCoreDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Student}/{action=List}/{id?}");
            });
        }
    }
 public class SchoolCoreDbContext : DbContext
    {
        public SchoolCoreDbContext(DbContextOptions<SchoolCoreDbContext> options) : base(options)
        {
        }
        public DbSet<Student> Student { get; set; }
    }
[Table("Student")]
    public class Student
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int StudentId { get; set; }

        [Required]
        [MinLength(3)]
        [MaxLength(30)]
        [Display(Name = "First Name")]
        public string FirstName { get; set; }

        [Required]
        [MinLength(3)]
        [MaxLength(30)]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }
    }
public class StudentViewModel
    {
        public Student Student { get; set; }
    }
public class StudentController : Controller
    {
        private readonly SchoolCoreDbContext _dbContext;

        public StudentController(SchoolCoreDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        public IActionResult List()
        {
            return View();
        }

        [HttpGet]
        public object GetStudentList(DataSourceLoadOptions loadOptions)
        {
            var lstStudents = _dbContext.Student.ToList();
            var vmStudent = new List<StudentViewModel>();

            foreach (var student in lstStudents)
            {
                vmStudent.Add(new StudentViewModel
                {
                    Student = student
                });
            }
            return vmStudent;
        }

        [HttpPost]
        public IActionResult StudentInsert(string values)
        {
            var vmStudent = new StudentViewModel();
            JsonConvert.PopulateObject(values, vmStudent);

            _dbContext.Student.Add(new Student {
                FirstName = vmStudent.Student.FirstName,
                LastName = vmStudent.Student.LastName
            });
            _dbContext.SaveChanges();
            return RedirectToAction("List");
        }

        [HttpPut]
        public IActionResult StudentUpdate(int key, string values)
        {
            var vmStudent = new StudentViewModel();
            JsonConvert.PopulateObject(values, vmStudent);
            var StudentUpdate = _dbContext.Student.First(s => s.StudentId == key);
            StudentUpdate.FirstName = vmStudent.Student.FirstName;
            StudentUpdate.LastName = vmStudent.Student.LastName;
            _dbContext.Update(StudentUpdate);
            _dbContext.SaveChanges();
            return RedirectToAction("List");
        }
        public IActionResult StudentDelete(int key, DataSourceLoadOptions loadOptions)
        {
            var StudentDelete = _dbContext.Student.First(s => s.StudentId == key);
            _dbContext.Student.Remove(StudentDelete);
            _dbContext.SaveChanges();
            return RedirectToAction("List");
        }
    }
@{
    ViewData["Title"] = "List";
    Layout = "~/Views/Shared/_DashBoard.cshtml";
}
@(Html.DevExtreme().DataGrid<SchoolCore.ViewModels.StudentViewModel>()
        .DataSource(ds => ds.Mvc()
            .Controller("Student")
            .LoadAction("GetStudentList")
            .InsertAction("StudentInsert")
            .UpdateAction("StudentUpdate")
            .DeleteAction("StudentDelete")
            .Key("Student.StudentId")
    )
    .RemoteOperations(true)
    .Columns(columns => {
        columns.AddFor(m => m.Student.StudentId);
        columns.AddFor(m => m.Student.FirstName);
        columns.AddFor(m => m.Student.LastName);
    })
    .Editing(e => e
        .AllowAdding(true)
        .AllowUpdating(true)
        .AllowDeleting(true)
    )
)
Read More