string.Format(“{0:s}.{0:fff}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123); String.Format("{0:y yy yyy yyyy}", dt); // "8 08 008 2008" year String.Format("{0:M MM MMM MMMM}", dt); // "3 03 Mar March" month String.Format("{0:d dd ddd dddd}", dt); // "9 09 Sun Sunday" day String.Format("{0:h hh H HH}", dt); // "4 04 16 16" hour 12/24 String.Format("{0:m mm}", dt); // "5 05" minute String.Format("{0:s ss}", dt); // "7 07" second String.Format("{0:f ff fff ffff}", dt); // "1 12 123 1230" sec.fraction String.Format("{0:F FF FFF FFFF}", dt); // "1 12 123 123" without zeroes String.Format("{0:t tt}", dt); // "P PM" A.M. or P.M. String.Format("{0:z zz zzz}", dt); // "-6 -06 -06:00" time zone |
1 2 3 4 |
String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9/3/2008 16:05:07" - english (en-US) String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9.3.2008 16:05:07" - german (de-DE) |
1 2 3 4 5 6 7 8 9 10 11 12 |
tring.Format("{0:M/d/yyyy}", dt); // "3/9/2008" String.Format("{0:MM/dd/yyyy}", dt); // "03/09/2008" // day/month names String.Format("{0:ddd, MMM d, yyyy}", dt); // "Sun, Mar 9, 2008" String.Format("{0:dddd, MMMM d, yyyy}", dt); // "Sunday, March 9, 2008" // two/four digit year String.Format("{0:MM/dd/yy}", dt); // "03/09/08" String.Format("{0:MM/dd/yyyy}", dt); // "03/09/2008" |
Standard DateTime Formatting In DateTimeFormatInfo there are defined standard patterns for the current culture. For example property ShortTimePattern is string that contains value h:mm tt for en-US culture and value HH:mm for de-DE culture. Following table shows patterns defined in DateTimeFormatInfo and their values for en-US culture. First column contains format specifiers… Read More »
MVC Download File and View
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Microsoft.Extensions.DependencyInjection; using Microsoft.AspNetCore; using Microsoft.Extensions.Configuration; using Microsoft.AspNetCore.Mvc; using System.Net.Http.Headers; namespace StartupBasic { public class Startup { public Startup(IHostingEnvironment env, ILoggerFactory logger, IConfiguration configuration) { //These are three services available at constructor } public void ConfigureServices(IServiceCollection services) { services.AddMvcCore(). SetCompatibilityVersion(CompatibilityVersion.Version_2_1); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory logger, IConfiguration configuration) { app.UseMvc(routes => routes.MapRoute( name: "default_route", template: "{controller}/{action}/{id?}", defaults: new { controller = "Home", action = "Index" }) ); } } public class HomeController : Controller { IHostingEnvironment _env; public HomeController(IHostingEnvironment env) { _env = env; } public ActionResult Index() { return new ContentResult { Content = "<html><body><h1>Download File</h1>Download a copy of <a href=\"/home/hegel\">Hegel (pdf)</a></body></html>", ContentType = "text/html" }; } //public FileStreamResult Hegel() //{ // var pathToIdeas = System.IO.Path.Combine(_env.WebRootPath, "hegel.pdf"); // //This is a contrite example to demonstrate returning a stream. If you have a physical file on disk, just use PhySicalFileResult that takes a path. // return new FileStreamResult(System.IO.File.OpenRead(pathToIdeas), "application/pdf") // { // FileDownloadName = "hegel.pdf" // }; //} public PhysicalFileResult Hegel() { var pathToIdeas = System.IO.Path.Combine(_env.WebRootPath, "hegel.pdf"); return new PhysicalFileResult(pathToIdeas, "application/pdf"); } } public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .UseEnvironment("Development"); } } |
결과 : Download File Download a copy of Hegel (pdf)
MVC routing-8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Microsoft.Extensions.DependencyInjection; using Microsoft.AspNetCore; using Microsoft.Extensions.Configuration; using Microsoft.AspNetCore.Mvc; namespace MvcRouting { public class Startup { public Startup(IHostingEnvironment env, ILoggerFactory logger, IConfiguration configuration) { //These are three services available at constructor } public void ConfigureServices(IServiceCollection services) { services.AddMvcCore(). SetCompatibilityVersion(CompatibilityVersion.Version_2_1); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory logger, IConfiguration configuration) { app.UseMvc(); } } [Route("[controller]/[action]")] public class HomeController : Controller { [HttpGet("/")] [HttpGet("")] public ActionResult Index() { return new ContentResult { Content = @" <html><body> <h1>[controller] and [action] replacement tokens examples</h1> <ul> <li><a href=""/"">/</a></li> <li><a href=""/home/index"">/home/index</a></li> <li><a href=""/home/about"">/home/about</a></li> <li><a href=""/about"">/about</a></li> </ul> </body></html>", ContentType = "text/html" }; } public ActionResult About() { return new ContentResult { Content = @" <html><body> <b>About Page</b </body></html>", ContentType = "text/html" }; } [HttpGet("/about")] public ActionResult About2() { return new ContentResult { Content = @" <html><body> <b>About Page 2</b </body></html>", ContentType = "text/html" }; } } public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .UseEnvironment("Development"); } } |
결과 : [controller] replacement token examples •/ •/home/ •/home/about •/about
MVC routing-7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Microsoft.Extensions.DependencyInjection; using Microsoft.AspNetCore; using Microsoft.Extensions.Configuration; using Microsoft.AspNetCore.Mvc; namespace MvcRouting { public class Startup { public Startup(IHostingEnvironment env, ILoggerFactory logger, IConfiguration configuration) { //These are three services available at constructor } public void ConfigureServices(IServiceCollection services) { services.AddMvcCore(). SetCompatibilityVersion(CompatibilityVersion.Version_2_1); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory logger, IConfiguration configuration) { app.UseMvc(); } } [Route("[controller]")] public class HomeController : Controller { [HttpGet("/")] [HttpGet("")] //You have to do this otherwise /Home/Index won't work public ActionResult Index() { return new ContentResult { Content = @" <html><body> <h1>[controller] replacement token examples</h1> <ul> <li><a href=""/"">/</a></li> <li><a href=""/home/"">/home/</a></li> <li><a href=""/home/about"">/home/about</a></li> <li><a href=""/about"">/about</a></li> </ul> </body></html>", ContentType = "text/html" }; } [HttpGet("about")] public ActionResult About() { return new ContentResult { Content = @" <html><body> <b>About Page</b </body></html>", ContentType = "text/html" }; } [HttpGet("/about")] public ActionResult About2() { return new ContentResult { Content = @" <html><body> <b>About Page 2</b </body></html>", ContentType = "text/html" }; } } public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .UseEnvironment("Development"); } } |
결과 : [controller] replacement token examples •/ •/home/ •/home/about •/about
MVC routing-1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Microsoft.Extensions.DependencyInjection; using Microsoft.AspNetCore; using Microsoft.Extensions.Configuration; using Microsoft.AspNetCore.Mvc; namespace MvcRouting { public class Startup { public Startup(IHostingEnvironment env, ILoggerFactory logger, IConfiguration configuration) { //These are three services available at constructor } public void ConfigureServices(IServiceCollection services) { services.AddMvcCore(). SetCompatibilityVersion(CompatibilityVersion.Version_2_1); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory logger, IConfiguration configuration) { app.UseMvc(routes => { routes.MapRoute( name: "home", template: "/", defaults: new { controller = "Home", action = "Index" }); routes.MapRoute( name: "default", template: "{controller}/{action}", defaults: new { controller = "Home", action = "Index" }); }); } } public class HomeController : Controller { public ActionResult Index() { return new ContentResult { Content = @" <html><body> <b>Hello World</b> <br/><br/> The following links call the same controller and action. <ul> <li><a href=""/"">/</a></li> <li><a href=""/Home"">/Home</a></li> <li><a href=""/Home/index"">/Home/index</a></li> </ul> </body></html>", ContentType = "text/html" }; } } public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .UseEnvironment("Development"); } } |
결과 : Hello World The following links call the same controller and action. •/ •/home •/home/index 동일 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
namespace MvcRouting { public class Startup { public Startup(IHostingEnvironment env, ILoggerFactory logger, IConfiguration configuration) { //These are three services available at constructor } public void ConfigureServices(IServiceCollection services) { services.AddMvcCore(). SetCompatibilityVersion(CompatibilityVersion.Version_2_1); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory logger, IConfiguration configuration) { app.UseMvc(); } } [Route("")] [Route("Home")] [Route("Home/Index")] public class HomeController : Controller { public ActionResult Index() { return new ContentResult { Content = @" <html><body> <b>Hello World</b> <br/><br/> The following links call the same controller and action. <ul> <li><a href=""/"">/</a></li> <li><a href=""/home"">/home</a></li> <li><a href=""/home/index"">/home/index</a></li> </ul> </body></html>", ContentType = "text/html" }; } } public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .UseEnvironment("Development"); } } |
MVC hello-world
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Microsoft.Extensions.DependencyInjection; using Microsoft.AspNetCore; using Microsoft.Extensions.Configuration; using Microsoft.AspNetCore.Mvc; namespace StartupBasic { public class Startup { public Startup(IHostingEnvironment env, ILoggerFactory logger, IConfiguration configuration) { //These are three services available at constructor } public void ConfigureServices(IServiceCollection services) { services.AddMvcCore(). SetCompatibilityVersion(CompatibilityVersion.Version_2_1); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory logger, IConfiguration configuration) { app.UseMvc(routes => routes.MapRoute( name: "default_route", template: "{controller}/{action}/{id?}", defaults: new { controller = "Home", action = "Index" }) ); } } public class HomeController : Controller { public ActionResult Index() { return new ContentResult { Content = "<html><body><b>Hello World</b></body></html>", ContentType = "text/html" }; } } public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .UseEnvironment("Development"); } } |
결과 : Hello World
mvc-output-xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Mvc.Formatters; namespace MvcOutputXml { public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddMvc(config => { config.OutputFormatters.Add(new XmlSerializerOutputFormatter()); }); } public void Configure(IApplicationBuilder app) { app.UseMvc(); } } public class Greeting { public bool Hello { get; set; } public bool World { get; set; } } public class HomeController : Controller { [HttpGet("")] public IActionResult Index() { var h = new Greeting { Hello = true, World = true }; Response.ContentType = "text/xml"; return Ok(h); } } public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .UseEnvironment("Development"); } } |
결과
1 2 3 4 5 6 7 |
<?xml version="1.0"?> <Greeting xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Hello>true</Hello> <World>true</World> </Greeting> |
visual studio 2017 net core 설치
1. 사이트 https://visualstudio.microsoft.com/ko/downloads/ 2. 다운로드 및 설치 3. 설치중 4. Net core 프로그램 선택 후 설치 5. 설치중
web-utilities-query-helpers-2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
using Microsoft.AspNetCore.Hosting; //IWebHostBuilder using Microsoft.AspNetCore.Builder; //IApplicationBuilder using Microsoft.AspNetCore.Http; //WriteAsync using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.AspNetCore; using Microsoft.Extensions.Configuration; using System.Collections.Generic; using Microsoft.AspNetCore.WebUtilities; //QueryHelpers namespace WebApplication1 { public class Startup { public Startup(IHostingEnvironment env, ILoggerFactory logger) { //These are two services available at constructor } public void ConfigureServices(IServiceCollection services) { //This is the only service available at ConfigureServices } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory logger, IConfiguration configuration) { app.Run(async context => { var queryString = QueryHelpers.ParseQuery(context.Request.QueryString.ToString()); var output = ""; foreach (var qs in queryString) { output += qs.Key + " = " + qs.Value + "<br/>"; } await context.Response.WriteAsync($@"<html> <body> <h1>Parsing Raw Query String</h1> <ul> <li><a href=""?name=anne"">?name=anne</a></li> <li><a href=""?name=anne&name=mishkind"">?name=anne&name=mishkind</a></li> <li><a href=""?age=25&smart=true"">?age=25&smart=true</a></li> <li><a href=""?country=zambia&country=senegal&country="">?country=zambia&country=senegal&country=</a></li> <li><a href=""?"">?</a></li> <br /><br /> <strong>Query String</strong><br/> {output} </ul> </body> </html>"); }); } } public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .UseEnvironment("Development"); } } |
결과 : Parsing Raw Query String •?name=anne •?name=anne&name=mishkind •?age=25&smart=true •?country=zambia&country=senegal&country= •? Query String name = anne,mishkind