Saturday 25 February 2017

AssetTracking 4 .aspcore web application consume webapi Cross-origin resource sharing




//~/assettracking/controllers/assetcontroller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using BLL;
using Data;
using Microsoft.AspNetCore.Mvc.Rendering;
using BLL.DTOs;
using System.Net.Http;
using System.Net.Http.Headers;

namespace AssetTracking.Controllers
{
    public class AssetController : Controller
    {
        asset_manager Manager;

        public AssetController(AssetContext context)
        {
            Manager = new asset_manager(context);    
        }

        // GET: Asset
        public ActionResult Index()
        {
            var asset_types_list = Manager.get_asset_types();

            ViewBag.asset_types_viewbag = new SelectList(asset_types_list.OrderBy(x=>x.Name), "Id", "Name");

            var assets = Manager.get_all();

            return View(assets);
        }

        public async Task<IActionResult> Assign()
        {
            var retValue = new List<employee_dto>();
            //make call to web api 
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:11111/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.
                    Add(new MediaTypeWithQualityHeaderValue("application/json"));

                HttpResponseMessage response = await client.GetAsync("api/values");
                if (response.IsSuccessStatusCode)
                {
                    retValue = await response.Content.ReadAsAsync<List<employee_dto>>();
                }
            }

            return View(retValue);
        }
    }
}

-------------------------------------------------------------

//~/HRService/controllers/valuescontroller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using HRService.Models;
using Microsoft.AspNetCore.Cors;

namespace HRService.Controllers
{
    //[Authorize]
    [EnableCors("AllowAllOrigin")]
    [Route("api/[controller]")]  
    public class ValuesController : Controller
    {
        HRContext DB;

        public ValuesController(HRContext context)
        {
            DB = context;
        }
 
        [HttpGet]
        public JsonResult get_all()
        {

            var employees = DB.Employee.Select(x => new employee_dto
            { Id = x.EmployeeNumber, name = x.FirstName + " " + x.LastName }).ToList();

            return Json( employees);

        }
    }
}

------------------------------------------------------------------

~/assettracking/projectjson

"Microsoft.VisualStudio.Web.CodeGenerators.Mvc": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Domain": "1.0.0-*",
    "BLL": "1.0.0-*",
    "Microsoft.AspNet.WebApi.Client": "5.2.3",
    "System.Net.Http": "4.3.0",
    "System.Runtime.Serialization.Xml": "4.3.0"
  },

-----------------------------------------------------------------

~/assettracking/views/assign

@model IEnumerable<BLL.DTOs.employee_dto>

<div id="employee_table">
        <table class="table">
            <thead>
                <tr>
                    <th>
                        @Html.DisplayNameFor(model => model.Id)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.name)
                    </th>

                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model)
                {
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => item.Id)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.name)
                        </td>

                    </tr>
                }
            </tbody>
        </table>
    </div>

---------------------------------------------------------

~/bll/dto/employee.dto

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BLL.DTOs
{
    public class employee_dto
    {
        public string Id { get; set; }
        public string name { get; set; }
    }
}

----------------------------------------------------------


reference:

http://stackoverflow.com/questions/9619776/how-to-create-a-listbox-in-html-without-allowing-multiple-selection
http://stackoverflow.com/questions/3854120/add-item-to-a-listbox-using-jquery
http://html.com/attributes/select-multiple/
http://stackoverflow.com/questions/18150954/how-can-i-render-a-list-select-box-dropdown-with-bootstrap
http://stackoverflow.com/questions/26845994/databinding-system-web-mvc-selectlistitem-does-not-contain-a-property-with-th
https://www.williamghelfi.com/blog/2013/06/09/bootstrap-3-the-new-grid-system-for-starters/
https://www.w3schools.com/tags/tag_center.asp
https://www.w3schools.com/bootstrap/bootstrap_buttons.asp
https://www.w3schools.com/jsref/event_onclick.asp
https://www.w3schools.com/jsref/event_onselect.asp
http://learn.jquery.com/using-jquery-core/faq/how-do-i-get-the-text-value-of-a-selected-option/
http://stackoverflow.com/questions/15997879/get-the-index-of-the-object-inside-an-array-matching-a-condition
https://www.w3schools.com/jsref/jsref_splice.asp
http://stackoverflow.com/questions/10024866/remove-object-from-array-using-javascript
http://stackoverflow.com/questions/5503900/how-to-sort-an-array-of-objects-with-jquery-or-javascript
http://stackoverflow.com/questions/7469088/html-can-i-display-button-text-in-multiple-lines
http://stackoverflow.com/questions/21578814/how-to-receive-json-as-an-mvc-5-action-method-parameter
http://stackoverflow.com/questions/22569877/how-to-redirect-to-another-action-in-an-action
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state

No comments:

Post a Comment