本文整理汇总了C#中IApplicationBuilder.MapWhen方法的典型用法代码示例。如果您正苦于以下问题:C# IApplicationBuilder.MapWhen方法的具体用法?C# IApplicationBuilder.MapWhen怎么用?C# IApplicationBuilder.MapWhen使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IApplicationBuilder
的用法示例。
在下文中一共展示了IApplicationBuilder.MapWhen方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Configure
public void Configure(IApplicationBuilder app, CommonService service)
{
var authOptions = new CookieAuthenticationOptions {
AuthenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme,
AutomaticAuthenticate = true,
AutomaticChallenge = true,
ExpireTimeSpan = TimeSpan.FromHours(5),
Events = new CookieAuthenticationEvents {
OnRedirectToLogin = context => {
context.Response.StatusCode = (int) HttpStatusCode.Unauthorized;
return Task.CompletedTask;
}
}
};
// redirect to setup until configured
app.MapWhen(
ctx => ctx.Request.Path == "/" && !service.IsConfigured(),
req => req.Run(
ctx => Task.Run(() => ctx.Response.Redirect("/setup"))
)
);
app.UseDefaultFiles()
.UseStaticFiles()
.UseCookieAuthentication(authOptions)
.UseMvc();
CreateDatabase(app);
}
示例2: Configure
public void Configure(IApplicationBuilder app, ILoggerFactory logger)
{
logger.AddConsole(LogLevel.Information);
app.UseHttpLog();
app.UseDeveloperExceptionPage();
app.UseStaticFiles();
app.UseMvc();
app.MapWhen(ctx => ctx.Request.Path.Value.Equals("/super-secret"), HandleSecret);
app.Use(async (context, next) =>
{
Console.WriteLine("1");
await next.Invoke();
Console.WriteLine("2");
});
app.Use(async (context, next) =>
{
Console.WriteLine("3");
await next.Invoke();
Console.WriteLine("4");
});
app.Run(async (context) =>
{
//throw new Exception("Something bad happened...");
await context.Response.WriteAsync(_configuration.Get<string>("HelloMessage"));
});
}
示例3: Configure
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
loggerFactory.MinimumLevel = LogLevel.Information;
loggerFactory.AddConsole();
app.MapWhen(ctx => ctx.Request.Path.Equals("/api/qrcode") && ctx.Request.Method.Equals("GET"), HandleQRCode);
app.Run(async (context) =>
{
context.Response.StatusCode = StatusCodes.Status400BadRequest;
await context.Response.WriteAsync("usage: /api/qrcode?text=yourtext");
});
}
示例4: Configure
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//
// Scenarios:
// 1. Multiple services.
// 2. Various versions or kinds of clients side by side.
//
//
// SMS
//
app.Map("/sms",
subApp =>
{
subApp.RunGateway(new GatewayOptions() { ServiceDescription = new SmsServiceDescription() });
}
);
//
// Counter
//
app.Map("/counter",
subApp =>
{
subApp.RunGateway(new GatewayOptions() { ServiceDescription = new CounterServiceDescription() });
}
);
app.Map("/Hosting/CounterService",
subApp =>
{
subApp.RunGateway(new GatewayOptions() { ServiceDescription = new CounterServiceDescription() });
}
);
app.MapWhen(
context =>
{
StringValues serviceNames;
return context.Request.Headers.TryGetValue("SF-ServiceName", out serviceNames) &&
serviceNames.Count == 1 &&
serviceNames[0] == "fabric:/Hosting/CounterService";
},
subApp =>
{
subApp.RunGateway(new GatewayOptions() { ServiceDescription = new CounterServiceDescription() });
}
);
}
示例5: Configure
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.MinimumLevel = LogLevel.Information;
loggerFactory.AddConsole(minLevel: LogLevel.Information);
loggerFactory.AddDebug();
var logger = loggerFactory.CreateLogger(env.EnvironmentName);
app.UseRequestLogger();
app.MapWhen(context =>
{
return context.Request.Query.ContainsKey("branch");
}, HandleBranch);
app.Run(async context =>
{
context.Response.ContentType = "text/plain";
await context.Response.WriteAsync("Hello from " + env.EnvironmentName);
});
}
示例6: Configure
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app,
IHostingEnvironment env)
{
app.UseIISPlatformHandler();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStatusCodePagesWithReExecute("/errors/{0}");
app.MapWhen(context => context.Request.Path == "/missingpage", builder => { });
// "/errors/400"
app.Map("/errors", error =>
{
error.Run(async context =>
{
var builder = new StringBuilder();
builder.AppendLine("<html><body>");
builder.AppendLine("An error occurred, Status Code: " +
HtmlEncoder.Default.HtmlEncode(context.Request.Path.ToString().Substring(1)) + "<br />");
var referrer = context.Request.Headers["referer"];
if (!string.IsNullOrEmpty(referrer))
{
builder.AppendLine("Return to <a href=\"" + HtmlEncoder.Default.HtmlEncode(referrer) + "\">" +
WebUtility.HtmlEncode(referrer) + "</a><br />");
}
builder.AppendLine("</body></html>");
context.Response.ContentType = "text/html";
await context.Response.WriteAsync(builder.ToString());
});
});
HomePage(app);
}
示例7: Configure_Map2
public void Configure_Map2(IApplicationBuilder app)
{
app.MapWhen(c => c.Request.Query["k"] == "1", a => a.Run(async c => await Dump(c.Response.Body, c.Request)));
app.MapWhen(c => c.Request.Query["k"] == "2", a => a.Run(async c => await Dump(c.Response.Body, c.Response)));
app.MapWhen(c => c.Request.Query["k"] == "3", a => a.Run(async c => await Dump(c.Response.Body, c)));
}
示例8: Configure
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
app.UseStaticFiles();
loggerFactory.AddConsole();
loggerFactory.AddDebug();
// loggerFactory.AddConsole(Configuration.GetSection("Logging"));
//loggerFactory.AddConsole();
// loggerFactory.AddDebug();
app.UseSession();
// uncomment these lines to use the Middleware sample
// app.UseHeaderMiddleware();
// app.UseHeading1Middleware();
app.Map("/home2", homeApp =>
{
homeApp.Run(async context =>
{
HomeController controller =
app.ApplicationServices.GetService<HomeController>();
int statusCode = await controller.Index(context);
context.Response.StatusCode = statusCode;
});
});
app.Map("/session", sessionApp =>
{
sessionApp.Run(async context =>
{
await SessionSample.SessionAsync(context);
});
});
PathString remaining;
app.MapWhen(context => context.Request.Path.StartsWithSegments("/configuration", out remaining),
configApp =>
{
configApp.Run(async context =>
{
if (remaining.StartsWithSegments("/appsettings"))
{
await ConfigSample.AppSettings(context, Configuration);
}
else if (remaining.StartsWithSegments("/database"))
{
await ConfigSample.ReadDatabaseConnection(context, Configuration);
}
else if (remaining.StartsWithSegments("/secret"))
{
await ConfigSample.UserSecret(context, Configuration);
}
});
});
app.Map("/configuration", configApp =>
{
configApp.Run(async context =>
{
await ConfigSample.ReadDatabaseConnection(context, Configuration);
});
});
//// uncomment this app.Run invocation to active the Hello, World! output
//app.Run(async (context) =>
//{
// await context.Response.WriteAsync("Hello World!");
//});
//// uncomment this app.Run invocation for the first Request and Response sample
//app.Run(async (context) =>
//{
// await context.Response.WriteAsync(RequestAndResponseSample.GetRequestInformation(context.Request));
//});
//// uncomment this app.Run invocation for the custom routing
//app.Run(async (context) =>
//{
// if (context.Request.Path.Value.ToLower() == "/home")
// {
// HomeController controller =
// app.ApplicationServices.GetService<HomeController>();
// int statusCode = await controller.Index(context);
// context.Response.StatusCode = statusCode;
// return;
// }
//}
//// uncomment this app.Run invocation for request/response samples
//app.Run(async (context) =>
//{
// string result = string.Empty;
// switch (context.Request.Path.Value.ToLower())
//.........这里部分代码省略.........
示例9: Configure
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseDeveloperExceptionPage();
// ...
app.UseMyMiddleware();
// ...
app.UseMyMiddlewareWithParams();
var myMiddlewareOptions =
Configuration.Get<MyMiddlewareOptions>("MyMiddlewareOptionsSection");
var myMiddlewareOptions2 =
Configuration.Get<MyMiddlewareOptions>("MyMiddlewareOptionsSection2");
app.UseMyMiddlewareWithParams(myMiddlewareOptions);
// ...
app.UseMyMiddlewareWithParams(myMiddlewareOptions2);
app.UseMyTerminatingMiddleware();
app.UseIISPlatformHandler();
// Create branch to the MyHandlerMiddleware.
// All requests ending in .report will follow this branch.
app.MapWhen(
context => context.Request.Path.ToString().EndsWith(".report"),
appBranch => {
// ... optionally add more middleware to this branch
appBranch.UseMyHandler();
});
app.MapWhen(
context => context.Request.Path.ToString().EndsWith(".context"),
appBranch => {
appBranch.UseHttpContextDemoMiddleware();
});
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
示例10: ConfigureMapWhen
public void ConfigureMapWhen(IApplicationBuilder app)
{
app.MapWhen(context => {
return context.Request.Query.ContainsKey("branch");
}, HandleBranch);
app.Run(async context =>
{
await context.Response.WriteAsync("Hello from " + _environment);
});
}
示例11: Configure
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseCors("AllowAll");
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationScheme = Startup.AuthenticationScheme,
CookieName = "KrosbookAuth",
AutomaticAuthenticate = true,
AutomaticChallenge = true,
SlidingExpiration = true,
ExpireTimeSpan = TimeSpan.FromHours(8),
LoginPath = new PathString("/login"),
AccessDeniedPath = new PathString("/login"),
CookieHttpOnly = true //ak je false tak mozno editovat cookie v prehliadaci
});
app.UseStaticFiles();
app.UseIdentity();
app.UseMvc();
app.MapWhen(context =>
{
var requestPath = context.Request.Path.Value;
return !(Path.HasExtension(requestPath) && requestPath.Contains("/api/"));
},
branch =>
{
branch.Use((context, next) =>
{
context.Request.Path = new PathString("/index.html");
return next();
});
branch.UseStaticFiles();
});
DbInitializer.Initialize(app.ApplicationServices);
}
示例12: Configure
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
app.UseIISPlatformHandler();
app.UseStaticFiles();
loggerFactory.AddConsole();
loggerFactory.AddDebug();
// loggerFactory.AddConsole(Configuration.GetSection("Logging"));
//loggerFactory.AddConsole();
// loggerFactory.AddDebug();
app.UseSession();
//// uncomment these lines to use the Middleware sample
//app.UseHeaderMiddleware();
//app.UseHeading1Middleware();
app.Map("/home2", homeApp =>
{
homeApp.Run(async context =>
{
HomeController controller =
app.ApplicationServices.GetService<HomeController>();
int statusCode = await controller.Index(context);
context.Response.StatusCode = statusCode;
});
});
app.Map("/session", sessionApp =>
{
sessionApp.Run(async context =>
{
await SessionSample.SessionAsync(context);
});
});
PathString remaining;
app.MapWhen(context => context.Request.Path.StartsWithSegments("/configuration", out remaining),
configApp =>
{
configApp.Run(async context =>
{
if (remaining.StartsWithSegments("/appsettings"))
{
await ConfigSample.AppSettings(context, Configuration);
}
else if (remaining.StartsWithSegments("/database"))
{
await ConfigSample.ReadDatabaseConnection(context, Configuration);
}
else if (remaining.StartsWithSegments("/secret"))
{
await ConfigSample.UserSecret(context, Configuration);
}
});
});
app.Map("/configuration", configApp =>
{
configApp.Run(async context =>
{
await ConfigSample.ReadDatabaseConnection(context, Configuration);
});
});
//// uncomment this app.Run invocation to active the Hello, World!output
//app.Run(async (context) =>
//{
// await context.Response.WriteAsync("Hello World!");
//});
//// uncomment this app.Run invocation for the first Request and Response sample
//app.Run(async (context) =>
//{
// await context.Response.WriteAsync(RequestAndResponseSample.GetRequestInformation(context.Request));
//});
//// uncomment this app.Run invocation for the custom routing
//app.Run(async (context) =>
//{
// if (context.Request.Path.Value.ToLower() == "/home")
// {
// HomeController controller =
// app.ApplicationServices.GetService<HomeController>();
// int statusCode = await controller.Index(context);
// context.Response.StatusCode = statusCode;
// return;
// }
// string result = string.Empty;
// switch (context.Request.Path.Value.ToLower())
// {
// case "/header":
// result = RequestAndResponseSample.GetHeaderInformation(context.Request);
//.........这里部分代码省略.........
示例13: Configure
bool IsTimingOn = true; // we would want this to be config, but that's another demo!
#endregion Fields
#region Methods
public void Configure(IApplicationBuilder app)
{
app
.MapWhen((context) => context.Request.Headers.ContainsKey("X-Request-Timing"), ConfigureTimedApi)
.Map("/api", ConfigureApi)
.Map("", ConfigureWeb);
}