Instantly share code, notes, and snippets.

View Categoriy.cs
public class Cate
{
public int Id { get; set; }
public int? ParentId { get; set; }
public string Name { get; set; }
}
var cats = new List<Cate>
{
new Cate {
View TryCatch.cs
try
{
throw new InvalidOperationException("Exception message");
}
catch (ArgumentNullException e)
{
_logger.LogError(e.Message, e.InnerException);
return View("Index");
}
View StartupApiWeb.cs
var path = context.Response.HttpContext.Request.Path;
if (path.HasValue && path.Value.Contains("api"))
{
context.Response.StatusCode = 500;
await context.Response.WriteAsync("An unexpected fault happened. Try again later.");
return;
}
context.Response.Redirect($"/Error/Status/{context.Response.StatusCode}");
View Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
View AspNetCoreGlobalExceptionHandlerExtension.cs
public static class AspNetCoreGlobalExceptionHandlerExtension
{
public static IApplicationBuilder UseAspNetCoreExceptionHandler(this IApplicationBuilder app)
{
var loggerFactory = app.ApplicationServices.GetService(typeof(ILoggerFactory)) as ILoggerFactory;
return app.UseExceptionHandler(HandleException(loggerFactory));
}
public static Action<IApplicationBuilder> HandleException(ILoggerFactory loggerFactory)
View ApiGlobalExceptionHandlerExtension.cs
public static class ApiGlobalExceptionHandlerExtension
{
public static IApplicationBuilder UseWebApiExceptionHandler(this IApplicationBuilder app)
{
var loggerFactory = app.ApplicationServices.GetService(typeof(ILoggerFactory)) as ILoggerFactory;
return app.UseExceptionHandler(HandleApiException(loggerFactory));
}
public static Action<IApplicationBuilder> HandleApiException(ILoggerFactory loggerFactory)
View ClientCredentials.cs
public async Task<IActionResult> CallApiUsingClientCredentials()
{
var tokenClient = new TokenClient("http://localhost:5000/connect/token", "mvc", "secret");
var tokenResponse = await tokenClient.RequestClientCredentialsAsync("Api1");
var client = new HttpClient();
client.SetBearerToken(tokenResponse.AccessToken);
var content = await client.GetStringAsync("http://localhost:5001/api/resource-without-policy");
return View("Json", content);
View AccessApi.cs
public async Task<IActionResult> CallApiUsingUserAccessToken()
{
var accessToken = await HttpContext.GetTokenAsync("access_token");
var client = new HttpClient();
client.SetBearerToken(accessToken);
var content = await client.GetStringAsync("http://localhost:5001/api/resource-with-policy");
return View("Json", content);
}
View ProfileService.cs
public class ProfileService : IProfileService
{
private readonly UserManager<ApplicationUser> _userManager;
public ProfileService(UserManager<ApplicationUser> userManager)
{
_userManager = userManager;
}
public Task GetProfileDataAsync(ProfileDataRequestContext context)
View Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// Add application services.