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