C#メモ
2100年1月1日金曜日
2020年1月1日水曜日
2018年6月18日月曜日
appsettings.jsonを取得
appsettings.json
Startup.cs
Models/Hogehoge.cs
Controllers/HomeController.cs
{
"Setting": {
"Name" : "FooBar",
"Age" : 20
}
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.Configure(Configuration.GetSection("Hogehoge"));
}
Models/Hogehoge.cs
public class Hogehoge
{
public string Name { get; set; }
public int Age { get; set; }
}
Controllers/HomeController.cs
public HomeController(IOptionsoption){ //option.Value.Name //option.Value.Age }
2018年5月15日火曜日
ルーティング
■.NetFramework
App_Start\RouteConfig.cs
■.Net Core
Startup.cs
App_Start\RouteConfig.cs
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
}
);
■.Net Core
Startup.cs
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
2018年5月14日月曜日
認証の設定
■.Net Framework
App_Start\IdentityConfig.cs
すでにある設定を変更する
■.Net Core
Startup.cs
App_Start\IdentityConfig.cs
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = false,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
すでにある設定を変更する
■.Net Core
Startup.cs
services.Configureオプションとして新たに追加する(options => { //パスワード設定 options.Password.RequiredLength = 6; options.Password.RequireDigit = true; options.Password.RequireNonAlphanumeric = false; options.Password.RequireLowercase = false; options.Password.RequireUppercase = false; });
2017年3月13日月曜日
回帰直線のメモ
using System;
using System.Collections.Generic;
namespace WindowsFormsApplication1
{
public class RegressionLine
{
public RegressionLine(Listdata)
{
this.Data = data;
this.CalcSlopeIntercept();
}
private List Data { get; set; }
public double Slope { get; private set; }
public double Intercept { get; private set; }
private void CalcSlopeIntercept()
{
double sumXY = 0;
double sumX = 0;
double sumY = 0;
double sumX2 = 0;
for (int i = 0; i < this.Data.Count; i++)
{
sumXY += Data[i].X * this.Data[i].Y;
sumX += Data[i].X;
sumY += this.Data[i].Y;
sumX2 += Math.Pow(Data[i].X, 2);
}
this.Slope = (this.Data.Count * sumXY - sumX * sumY) / (this.Data.Count * sumX2 - Math.Pow(sumX, 2));
this.Intercept = (sumX2 * sumY - sumXY * sumX) / (this.Data.Count * sumX2 - Math.Pow(sumX, 2));
}
public double GetExpectancyX(double y)
{
return (y - this.Intercept) / this.Slope;
}
public double GetExpectancyY(double x)
{
return this.Slope * x + this.Intercept;
}
}
}
2017年2月21日火曜日
大文字、小文字を無視してDistinct
小文字でグループ化したデータのうち最初のデータを取り出す
実行すると CSharpが出力される
using System;
using System.Collections.Generic;
using System.Linq;
public class Hello{
public static void Main(){
// Here your code !
List data = new List{"CSharp", "CSHARP", "csharp"};
foreach(string d in data.GroupBy(d => d.ToLower()).Select(d => d.First())){
Console.WriteLine(d);
}
}
}
実行すると CSharpが出力される
登録:
コメント (Atom)