Tuesday 14 March 2017

.aspcore sql visual studio 2017 publish

after publish on server, able to access my website via smartphone


create database first project, reference:

//controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using publish_test.Models;

namespace publish_test.Controllers
{
    public class dataController : Controller
    {
        testdataContext DB;

        public dataController(testdataContext context)
        {
            DB = context;
        }

        // GET: data
        public ActionResult Index()
        {
            var schools = DB.School.ToList();

            return View(schools);
        }

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

//view


@model IEnumerable<publish_test.Models.School>

    <h2>School table </h2>

    <table class="table table-striped">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Id)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Name)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Location)
                </th>
            </tr>
        </thead>

        <tbody>
            @foreach (var item in Model)
            {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Id)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Location)
                </td>
            </tr>
            }
        </tbody>
    </table>

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

//testdataContext

using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

namespace publish_test.Models
{
    public partial class testdataContext : DbContext
    {
        public virtual DbSet<School> School { get; set; }

        public testdataContext(DbContextOptions<testdataContext> options) : base(options){ }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<School>(entity =>
            {
                entity.ToTable("school");

                entity.Property(e => e.Id).HasColumnName("id");

                entity.Property(e => e.Location)
                    .HasColumnName("location")
                    .HasColumnType("nchar(10)");

                entity.Property(e => e.Name)
                    .HasColumnName("name")
                    .HasColumnType("nchar(10)");
            });
        }
    }
}

--------------------------------------------------------
//school.cs

using System;
using System.Collections.Generic;

namespace publish_test.Models
{
    public partial class School
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Location { get; set; }
    }
}

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

publish to iis


next

save


myweb is published on iis, iis setup reference:

add iis to sql user, reference:




-------------------------------------------------------------------------------------------------------
//not related to publish
//visual studio 2017 dependency System.Net.Http has conflict with Microsoft.AspNet.WebApi.Client
//use method alternative to ReadAsAsync

public async Task<IActionResult> Load_employees_from_HR()
        {
            employees_from_HR = 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)
                {
                    //employees_from_HR = await response.Content.ReadAsAsync<List<employee_dto>>();
                    var stringResult = await response.Content.ReadAsStringAsync();
                    employees_from_HR = JsonConvert.DeserializeObject<List<employee_dto>>(stringResult);
                }
            }

            //employees_from_HR.Add(new employee_dto { Id = "", name = "" });

            return Content("employees are loaded from HR");
        }


reference:
https://docs.microsoft.com/en-us/aspnet/core/publishing/iis
http://stackoverflow.com/questions/6167648/visual-studio-reformat-code-document
http://stackoverflow.com/questions/40699424/project-json-not-found-in-visual-studio-2017-rc-solution-explorer
https://wildermuth.com/2017/02/11/Updating-My-Blog-to-Visual-Studio-2017-and-csproj
http://stackoverflow.com/questions/30400358/cant-find-system-net-http-formatting-dll
https://jonhilton.net/2017/01/24/retrieve-data-from-a-third-party-openweather-api-using-asp-net-core-web-api/

No comments:

Post a Comment