Skip to content

Instantly share code, notes, and snippets.

Avatar
:shipit:

Scott Sauber scottsauber

:shipit:
View GitHub Profile
View CounterTests.cs
public class CounterTests : TestContext
{
[Fact]
public void ShouldIncrementCountWhenButtonIsClicked()
{
var counterComponent = RenderComponent<Counter>();
counterComponent.Find("button").Click();
// This assertion tests that the state has incremented by 1
View Counter.razor
@page "/counter"
<h1>Counter</h1>
<p id="current-count">Current count: @CurrentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
public int CurrentCount { get; set; }
View ConfigurationBuilderExample.cs
var configurationBuilder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{Environment}.json");
if (Environment.IsDevelopment())
{
configurationBuilder.AddUserSecrets();
}
configurationBuilder.AddEnvironmentVariables();
View Index.cshtml.cs
[Authorize(PolicyName="Admin")]
public class IndexModel : PageModel
{
public void OnGet()
{
// do something
}
}
View Index.cshtml.cs
[Authorize]
public class IndexModel : PageModel
{
public void OnGet()
{
// do something
}
}
View Startup.cs
// OLD WAY FROM ASP.NET CORE PRIOR TO 3.0!!!!!!!!!
public class Startup
{
// Other Startup code omitted
public void ConfigureServices(IServiceCollection services)
{
// OLD WAY FROM ASP.NET CORE PRIOR TO 3.0!!!!!!!!!
services.AddMvc(options =>
{
var policy = new AuthorizationPolicyBuilder()
View Startup.cs
public class Startup
{
// Other Startup code omitted
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthorization(options =>
{
options.FallbackPolicy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
View Startup.cs
public class Startup
{
private readonly IHostingEnvironment _hostingEnvironment;
public Startup(IHostingEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
}
public void ConfigureServices(IServiceCollection services)
View Startup.cs
public class Startup
{
private readonly IWebHostEnvironment _environment;
public Startup(IWebHostEnvironment environment)
{
_environment = environment;
}
public void ConfigureServices(IServiceCollection services)
View Startup.cs
// ASP.NET Core 2.2 and earlier
public void ConfigureServices(IServiceCollection services, IHostingEnvironment hostingEnvironment)
{
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddJsonOptions(options =>
{
options.SerializerSettings.Formatting = hostingEnvironment.IsDevelopment() ? Formatting.Indented : Formatting.None;
});
}