Sunday, October 6, 2019

Call Constructor Api by controller or Call async Api by controller or Call async method or get async method or call async web api, call core api

Call Constructor Api by controller or Call async Api by controller or Call async method or get async method or call async web api, call core api


1.Web Api
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WebApplication8.Data;
using WebApplication8.Models;

namespace WebApplication8.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class customers_apiController : ControllerBase
    {
        private readonly ApplicationDbContext _context;
        public customers_apiController(ApplicationDbContext context)
        {
            _context = context;
        }
        // GET: api/customers_api
        [HttpGet]
        public async Task<List<customer>> Getcustomer([FromQuery] customer d)
        {
            var data = from x in _context.customer
                       where (d.Cust_ID == null || d.Cust_ID == x.Cust_ID)
                       && (d.Cust_Name == null || x.Cust_Name.Contains(d.Cust_Name))
                       orderby x.Cust_Name
                       select x;
            return await data.ToListAsync();
        }
    }
}

2. Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using WebApplication8.Data;
using WebApplication8.Models;

namespace WebApplication8.Controllers
{
    public class customersController : Controller
    {
        private readonly ApplicationDbContext _context;       
        customers_apiController api;
        public customersController(ApplicationDbContext context)
        {
            _context = context;
            api = new customers_apiController(_context);
        }
        // GET: customers     
        public async Task<IActionResult> Index(customer d)
        {           
            List<customer> data = await api.Getcustomer(d);
            return View(data);
        }
    }
}

No comments:

Post a Comment