.net core入门

各种坑还没填完,但是还是走上新的学习之路,性能非常好的.net core http://www.techempower.com/benchmarks/

怎么写出一个页面并给相应的方法呢,我是照葫芦画瓢,在Weather forecast的基础上写了MessageBoard

import React, { Component } from 'react';

export class MessageBoard extends Component {

    static displayName = MessageBoard.name;

    constructor(props) {
        super(props);
        this.state = { messageboards: [], loading: true };
    }

    componentDidMount() {
        this.populateWeatherData();
    }

    static renderForecastsTable(messageboards) {
        return (
            <table className='table table-striped'>
                <thead>
                    <tr>
                        <th>title</th>
                        <th>name</th>
                        <th>message</th>
                        <th>leaveDate</th>
                    </tr>
                </thead>
                <tbody>
                    {messageboards.map(messageboard =>
                        <tr key={messageboard.name}>
                            <td>{messageboard.title}</td>
                            <td>{messageboard.name}</td>
                            <td>{messageboard.message}</td>
                            <td>{messageboard.leaveDate}</td>
                        </tr>
                    )}
                </tbody>
            </table>
        );
    }

    render() {
        let contents = this.state.loading
            ? <p><em>Loading...</em></p>
            : MessageBoard.renderForecastsTable(this.state.messageboards);

        return (
            <div>
                <h1>留言板呀</h1>
                <p>这就是服务器所有的留言</p>
                {contents}
            </div>
        );
    }

    async populateWeatherData() {
        const response = await fetch('Messageboard');
        const data = await response.json();
        this.setState({ messageboards: data, loading: false });
    }
}
MessageBoard.js

并在NavMenu.js加入<NavLink tag={Link} className="text-dark" to="/message-board">Messageboard</NavLink>

还有App.js加入 <Route path='/message-board' component={MessageBoard} /> 当然要先import

react就是这样,接下来就是我们的类和controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using MySql.Data.MySqlClient;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace HelloDemo.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class MessageBoardController : ControllerBase
    {
        private readonly ILogger<MessageBoardController> _logger;

        public MessageBoardController(ILogger<MessageBoardController> logger)
        {
            _logger = logger;
        }
        [HttpGet]
        public IEnumerable<MessageBoard> Get()
        {
            List<MessageBoard> list = new List<MessageBoard>();
            //连接数据库
            MySqlConnection connection = new MySqlConnection
            {
                ConnectionString = "server = bobhuang.xyz; userid = root; password = root; database = messageboards; "
            };
            connection.Open();
            //查找数据库里面的表
            MySqlCommand mscommand = new MySqlCommand("select * from messages", connection);
            using (MySqlDataReader reader = mscommand.ExecuteReader())
            {
                //读取数据
                while (reader.Read())
                {
                    list.Add(new MessageBoard()
                    {
                        Name = reader.GetString("name"),
                        Title = reader.GetString("title"),
                        Message = reader.GetString("message"),
                        LeaveDate = reader.GetDateTime("created_at")
                    });
                }
            }
            return list.ToArray();
        }
    }
}
MessageBoardController.cs
using System;
namespace HelloDemo
{
    public class MessageBoard
    {
        public string Name { get; set; }

        public string Title { get; set; }

        public string Message { get; set; }

        public DateTime LeaveDate { get; set; }
    }
}
MessageBoard.cs

连接数据库需要导入包

然后还有些厉害的操作

ICollection<T> 同时继承IEnumerable<T>和IEnumerable两个接口

IList 继承它们三个接口,List 是类,不仅实现它们的接口,而且还扩展了很多的方法

按照功能排序:List < IList < ICollection < IEnumerable

按照性能排序:IEnumerable < ICollection < IList < List

using语句

定义一个范围,在范围结束时处理对象。 
场景: 
当在某个代码段中使用了类的实例,而希望无论因为什么原因,只要离开了这个代码段就自动调用这个类实例的Dispose。 
要达到这样的目的,用try...catch来捕捉异常也是可以的,但用using也很方便。

json里英语还用英语,中文用的是\u+四位十六进制数

\u则代表unicode编码,是一个字符;
0x开头代表十六进制,实际上就是一个整数;

\U607C代码汉子烦

DecimalUTF-8UTF-16UTF-32
24700E6 81 BC 607C0000607C

 

字符编码笔记:ASCII,Unicode 和 UTF-8 所谓编码--泛谈ASCII、Unicode、UTF8、UTF16、UCS-2等编码格式

就这样莫名其妙学了一波前端的内容,js使用的是usc-2,utf-16是usc-2的超集,用两位表示比如emoj。存在辅助平面,如果\ud800-\udbff之间,则需要与后一个字符放在一起,js可以解析,

比如UTF-16:D869 DEA5,UTF-32:0002A6A5

 

可以直接访问controller获取json数据是否安全?

接下来就是去学习Microsoft Azure,一堆骚操作,keyvault、cosmos db、app service

转载于:https://www.cnblogs.com/BobHuang/p/11252380.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值