Sessions in .Net Core

Sessions will not work out of the box, you need to do some setup:

  1. Add the nuget package Microsoft.AspNetCore.Session

  2. In Startup.cs add to ConfigureServices

    1
    2
    3
    services.AddSession(options => {
    options.IdleTimeout = TimeSpan.FromMinutes(15);
    });
  3. In Startup.cs add to Configure

    1
    app.UseSession();
  4. Then in your controllers you can get and set values, would be best to have ProjectName set in a constants file.

    1
    2
    3
    HttpContext.Session.SetString("ProjectName", project.ProjectName);

    var projectName = HttpContext.Session.GetString("ProjectName");

Microsoft.AspNetCore.Http

These are the methods available for use in the controller (Version=2.2.0.0)

1
2
3
4
5
public static byte[] Get(this ISession session, string key);
public static int? GetInt32(this ISession session, string key);
public static string GetString(this ISession session, string key);
public static void SetInt32(this ISession session, string key, int value);
public static void SetString(this ISession session, string key, string value);

References

Factory Pattern

Definition

There are 2 factory patterns as described by the Gang of Four.

Abstract Factory

The abstract factory pattern is used to provide a client with a set of related or dependent objects. The “family” of objects created by the factory are determined at run-time.

Factory Method

The factory pattern is used to replace class constructors, abstracting the process of object generation so that the type of the object instantiated can be determined at run-time.

References

Gang of Four Design Patterns

The Gang of Four are the authors of the book, “Design Patterns: Elements of Reusable Object-Oriented Software“. These patterns are grouped as follows

Creational Patterns

Structural Patterns

  • Adapter
  • Bridge
  • Composite
  • Decorator
  • Facade
  • Flyweight
  • Proxy

Behavioural Patterns

  • Chain of Responsibility
  • Command
  • Interpreter
  • Iterator
  • Mediator
  • Memento
  • Observer
  • State
  • Strategy
  • Template Method
  • Visitor

References

Upload File MVC Core

.Net Core 2x

Upload and save some file.

FileUploadViewModel

1
2
3
4
public string Foo { get; set; }

[Display(Name = "Some File")]
public List<IFormFile> FormFiles { get; set; }

FileUploadController

1
2
3
4
5
6
7
8
9
10
11
12
13
// POST: FileUpload/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("PackageType,FormFiles")] FileUploadViewModel fileUploadViewModel)
{
if (ModelState.IsValid)
{
var postedFile = fileUploadViewModel.FormFiles[0];

using (var memoryStream = new MemoryStream())
{
postedFile.CopyTo(memoryStream);
...

View Create.cshtml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<form asp-action="Create" enctype="multipart/form-data">
<div class="form-group">
<label asp-for="Foo" class="control-label"></label>
<input asp-for="Foo" class="form-control" />
<span asp-validation-for="PackageType" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="FormFiles" class="control-label"></label>
<input asp-for="FormFiles" type="file" multiple>
<span asp-validation-for="FormFiles" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Upload" class="btn btn-primary" />
</div>
</form>

References

Colab

Colab Notebooks. Colaboratory is a Google research project created to help disseminate machine learning education and research. It’s a Jupyter notebook environment that requires no setup to use and runs entirely in the cloud.

Shortcuts

List of most useful commands :

RUNNING

  • Ctrl + ENTER : Run current cell
  • Shift + ENTER : Run current cell & select next cell or create new one if last
  • Alt + ENTER : Run current cell & create new cell
  • Ctrl + F9 : Run all cells in notebook

HANDLING CELLS

  • Ctrl + M + A : Create new cell above current cell
  • Ctrl + M + B : Create new cell below current cell
  • Ctrl + M + M : Transform current Code cell into Text cell
  • Ctrl + M + Y : Transform current Text cell into Code cell
  • Ctrl + M + P : Go to previous cell

References