Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE TABLE users (
- ID int NOT NULL AUTO_INCREMENT,
- LastName varchar(255) NOT NULL,
- FirstName varchar(255),
- Address varchar(255),
- City varchar(255),
- ZipCode varchar(255),
- Country varchar(255),
- Email varchar(255),
- AvatarURL varchar(255),
- PRIMARY KEY (ID)
- );
- {
- "ConnectionStrings": {
- "MySQL": "server=localhost;port=3306;user=xxx;password=xxx;database=xxx"
- },
- "Logging": {
- "LogLevel": {
- "Default": "Information",
- "Microsoft": "Warning",
- "Microsoft.Hosting.Lifetime": "Information"
- }
- },
- "AllowedHosts": "*"
- }
- private readonly IConfiguration _configuration;
- public UserController(IConfiguration configuration)
- {
- _configuration = configuration;
- }
- [HttpGet]
- [Route("users")]
- public JsonResult Get()
- {
- string query = @"select * from users";
- string sqlDataSource = _configuration.GetConnectionString("MySQL");
- DataTable table = new DataTable();
- MySqlDataReader myReader;
- using (MySqlConnection myCon = new MySqlConnection(sqlDataSource))
- {
- myCon.Open();
- using (MySqlCommand myCommand = new MySqlCommand(query, myCon))
- {
- myReader = myCommand.ExecuteReader();
- table.Load(myReader);
- myReader.Close();
- myCon.Close();
- }
- }
- return new JsonResult(table);
- }
- public void ConfigureServices(IServiceCollection services)
- {
- //Enable CORS
- services.AddCors(c =>
- {
- c.AddPolicy("AllowOrigin", options => options.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
- });
- //JSON Serializer
- services.AddControllersWithViews().AddNewtonsoftJson(options =>
- options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore)
- .AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver
- = new DefaultContractResolver());
- services.AddControllers();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement