本文整理汇总了C#中Microsoft.AspNet.Hosting.WebApplicationBuilder类的典型用法代码示例。如果您正苦于以下问题:C# WebApplicationBuilder类的具体用法?C# WebApplicationBuilder怎么用?C# WebApplicationBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
WebApplicationBuilder类属于Microsoft.AspNet.Hosting命名空间,在下文中一共展示了WebApplicationBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetDefaultCultureInfoIfCultureKeysAreMissing
public async void GetDefaultCultureInfoIfCultureKeysAreMissing()
{
var builder = new WebApplicationBuilder()
.Configure(app =>
{
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US")
});
app.Run(context =>
{
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
var requestCulture = requestCultureFeature.RequestCulture;
Assert.Equal("en-US", requestCulture.Culture.Name);
Assert.Equal("en-US", requestCulture.UICulture.Name);
return Task.FromResult(0);
});
});
using (var server = new TestServer(builder))
{
var client = server.CreateClient();
var response = await client.GetAsync("/page");
}
}
示例2: Main
public static void Main(string[] args)
{
if (Environment.GetEnvironmentVariable("ASPNET_ENV") == "Debug")
{
// Build an ASP.NET 5 web application that serves as the communication listener.
var webApp = new WebApplicationBuilder().UseConfiguration(WebApplicationConfiguration.GetDefault())
.ConfigureLogging(factory =>
{
factory.AddConsole();
})
.UseStartup<Startup>()
.Build();
// Replace the address with the one dynamically allocated by Service Fabric.
webApp.GetAddresses().Clear();
webApp.GetAddresses().Add(ListeningAddress);
webApp.Run();
Thread.Sleep(Timeout.Infinite);
}
else
{
using (var fabricRuntime = FabricRuntime.Create())
{
fabricRuntime.RegisterServiceType("MicroserviceTemplateType", typeof(MicroserviceTemplate));
Thread.Sleep(Timeout.Infinite);
}
}
}
示例3: XForwardedForOverrideBadIpDoesntChangeRemoteIp
public async Task XForwardedForOverrideBadIpDoesntChangeRemoteIp()
{
var assertsExecuted = false;
var builder = new WebApplicationBuilder()
.Configure(app =>
{
app.UseOverrideHeaders(new OverrideHeaderOptions
{
ForwardedOptions = ForwardedHeaders.XForwardedFor
});
app.Run(context =>
{
Assert.Null(context.Connection.RemoteIpAddress);
assertsExecuted = true;
return Task.FromResult(0);
});
});
var server = new TestServer(builder);
var req = new HttpRequestMessage(HttpMethod.Get, "");
req.Headers.Add("X-Forwarded-For", "BAD-IP");
await server.CreateClient().SendAsync(req);
Assert.True(assertsExecuted);
}
示例4: CustomServiceProviderSetsApplicationServices
public async Task CustomServiceProviderSetsApplicationServices()
{
var builder = new WebApplicationBuilder().UseStartup<CustomContainerStartup>();
var server = new TestServer(builder);
string result = await server.CreateClient().GetStringAsync("/path");
Assert.Equal("ApplicationServicesEqual:True", result);
}
示例5: Main
public static void Main(string[] args)
{
var application = new WebApplicationBuilder()
.UseServer("Microsoft.AspNet.Server.Kestrel") // Set the server manually
.UseApplicationBasePath(Directory.GetCurrentDirectory()) // Override the application base with the current directory
.UseUrls("http://*:1000", "https://*:902")
.UseEnvironment("Development")
.UseWebRoot("public")
.ConfigureServices(services =>
{
// Configure services that the application can see
services.AddSingleton<IMyCustomService, MyCustomService>();
})
.Configure(app =>
{
// Write the application inline, this won't call any startup class in the assembly
app.Use(next => context =>
{
return next(context);
});
})
.Build();
application.Run();
}
示例6: BufferResponseWithManualContentLength_NotReplaced
public async Task BufferResponseWithManualContentLength_NotReplaced()
{
var builder = new WebApplicationBuilder()
.Configure(app =>
{
app.UseResponseBuffering();
app.Run(async context =>
{
context.Response.ContentLength = 12;
Assert.False(context.Response.HasStarted);
Assert.True(context.Response.Body.CanSeek);
await context.Response.WriteAsync("Hello World");
Assert.False(context.Response.HasStarted);
Assert.True(context.Response.Body.CanSeek);
});
});
var server = new TestServer(builder);
var response = await server.CreateClient().GetAsync("");
response.EnsureSuccessStatusCode();
Assert.Equal("Hello World", await response.Content.ReadAsStringAsync());
IEnumerable<string> values;
Assert.True(response.Content.Headers.TryGetValues("Content-Length", out values));
Assert.Equal("12", values.FirstOrDefault());
}
示例7: CorsRequest_MatchPolicy_SetsResponseHeaders
public async Task CorsRequest_MatchPolicy_SetsResponseHeaders()
{
// Arrange
var appBuilder = new WebApplicationBuilder()
.Configure(app =>
{
app.UseCors(builder =>
builder.WithOrigins("http://localhost:5001")
.WithMethods("PUT")
.WithHeaders("Header1")
.WithExposedHeaders("AllowedHeader"));
app.Run(async context =>
{
await context.Response.WriteAsync("Cross origin response");
});
})
.ConfigureServices(services => services.AddCors());
using (var server = new TestServer(appBuilder))
{
// Act
// Actual request.
var response = await server.CreateRequest("/")
.AddHeader(CorsConstants.Origin, "http://localhost:5001")
.SendAsync("PUT");
// Assert
response.EnsureSuccessStatusCode();
Assert.Equal(2, response.Headers.Count());
Assert.Equal("Cross origin response", await response.Content.ReadAsStringAsync());
Assert.Equal("http://localhost:5001", response.Headers.GetValues(CorsConstants.AccessControlAllowOrigin).FirstOrDefault());
Assert.Equal("AllowedHeader", response.Headers.GetValues(CorsConstants.AccessControlExposeHeaders).FirstOrDefault());
}
}
示例8: RegisterAddresses_Success
public async Task RegisterAddresses_Success(string addressInput, string[] testUrls)
{
var config = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{ "server.urls", addressInput }
})
.Build();
var applicationBuilder = new WebApplicationBuilder()
.UseConfiguration(config)
.UseServerFactory("Microsoft.AspNet.Server.Kestrel")
.Configure(ConfigureEchoAddress);
using (var app = applicationBuilder.Build().Start())
{
using (var client = new HttpClient())
{
foreach (var testUrl in testUrls)
{
var responseText = await client.GetStringAsync(testUrl);
Assert.Equal(testUrl, responseText);
}
}
}
}
示例9: GetFallbackLanguage_ReturnsFromSupportedCulture_AcceptLanguageListContainsSupportedCultures
public async void GetFallbackLanguage_ReturnsFromSupportedCulture_AcceptLanguageListContainsSupportedCultures()
{
var builder = new WebApplicationBuilder()
.Configure(app =>
{
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("fr-FR"),
SupportedCultures = new List<CultureInfo>
{
new CultureInfo("ar-SA"),
new CultureInfo("en-US")
}
});
app.Run(context =>
{
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
var requestCulture = requestCultureFeature.RequestCulture;
Assert.Equal("ar-SA", requestCulture.Culture.Name);
return Task.FromResult(0);
});
});
using (var server = new TestServer(builder))
{
var client = server.CreateClient();
client.DefaultRequestHeaders.AcceptLanguage.ParseAdd("en-GB,ar-SA,en-US");
var count = client.DefaultRequestHeaders.AcceptLanguage.Count;
var response = await client.GetAsync(string.Empty);
}
}
示例10: GetCultureInfoFromQueryString
public async void GetCultureInfoFromQueryString()
{
var builder = new WebApplicationBuilder()
.Configure(app =>
{
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = new List<CultureInfo>
{
new CultureInfo("ar-SA")
},
SupportedUICultures = new List<CultureInfo>
{
new CultureInfo("ar-YE")
}
});
app.Run(context =>
{
var requestCultureFeature = context.Features.Get<IRequestCultureFeature>();
var requestCulture = requestCultureFeature.RequestCulture;
Assert.Equal("ar-SA", requestCulture.Culture.Name);
Assert.Equal("ar-YE", requestCulture.UICulture.Name);
return Task.FromResult(0);
});
});
using (var server = new TestServer(builder))
{
var client = server.CreateClient();
var response = await client.GetAsync("/page?culture=ar-SA&ui-culture=ar-YE");
}
}
示例11: Main
public static void Main(string[] args)
{
var application = new WebApplicationBuilder()
.UseConfiguration(WebApplicationConfiguration.GetDefault(args))
.UseStartup<Startup>()
.Build();
application.Run();
}
示例12: Main
// Entry point for the application.
public static void Main(string[] args)
{
var app = new WebApplicationBuilder()
.UseServer("Microsoft.AspNet.Server.Kestrel")
.UseConfiguration(WebApplicationConfiguration.GetDefault(args))
.UseStartup<Startup>()
.Build();
app.Run();
}
示例13: ReturnsNotFoundWithoutWwwroot
public async Task ReturnsNotFoundWithoutWwwroot()
{
var builder = new WebApplicationBuilder()
.Configure(app => app.UseStaticFiles());
var server = new TestServer(builder);
var response = await server.CreateClient().GetAsync("/ranges.txt");
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}
示例14: LargeDownload
public async Task LargeDownload()
{
var config = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{ "server.urls", "http://localhost:8792/" }
})
.Build();
var applicationBuilder = new WebApplicationBuilder()
.UseConfiguration(config)
.UseServerFactory("Microsoft.AspNet.Server.Kestrel")
.Configure(app =>
{
app.Run(async context =>
{
var bytes = new byte[1024];
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = (byte)i;
}
context.Response.ContentLength = bytes.Length * 1024;
for (int i = 0; i < 1024; i++)
{
await context.Response.Body.WriteAsync(bytes, 0, bytes.Length);
}
});
});
using (var app = applicationBuilder.Build().Start())
{
using (var client = new HttpClient())
{
var response = await client.GetAsync("http://localhost:8792/");
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStreamAsync();
// Read the full response body
var total = 0;
var bytes = new byte[1024];
var count = await responseBody.ReadAsync(bytes, 0, bytes.Length);
while (count > 0)
{
for (int i = 0; i < count; i++)
{
Assert.Equal(total % 256, bytes[i]);
total++;
}
count = await responseBody.ReadAsync(bytes, 0, bytes.Length);
}
}
}
}
示例15: AddRequestHeader
public void AddRequestHeader()
{
var builder = new WebApplicationBuilder().Configure(app => { });
var server = new TestServer(builder);
server.CreateRequest("/")
.AddHeader("Host", "MyHost:90")
.And(request =>
{
Assert.Equal("MyHost:90", request.Headers.Host.ToString());
});
}