ASP.NET Core Runtime Compilation
Package: Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
Program.cs page add
// this will provide ablity to referesh pages at runtime
builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation();
ASP.NET Core 6 Database Connectivity
Package:
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
Microsoft.EntityFrameworkCore.Design
Scaffold-DbContext "Data Source=Server_Name; Initial Catalog=DataBase_Name; User ID=sa; Password=123456" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -f
ASP.NET Core 6 Session
Package: Microsoft.AspNetCore.Session
In program.cs file
builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation();
builder.Services.AddDistributedMemoryCache(); // for session
builder.Services.AddSession(options => {
options.IdleTimeout = TimeSpan.FromMinutes(20);//You can set Time
}); // for session
builder.Services.AddHttpContextAccessor();
builder.Services.AddSingleton
();
app.UseSession(); // for session
// before below code
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
Get Session in View
Add Below in Top
@using Microsoft.AspNetCore.Http
@inject IHttpContextAccessor HttpContextAccessor
For Accessing Session
@HttpContextAccessor.HttpContext.Session.GetString("SessionName")
Get Session in Controller
For Accessing Session
HttpContext.Session.GetInt32("SessionName");
Get and Set a Class in Session
Create a SessionHelper.cs file with below code
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
public static class SessionHelper
{
public static void SetObjectInSession(this ISession session, string key, object value)
{
string str = JsonConvert.SerializeObject(value);
session.SetString(key, JsonConvert.SerializeObject(value));
}
public static T GetCustomObjectFromSession(this ISession session, string key)
{
var value = session.GetString(key);
return value == null ? default(T) : JsonConvert.DeserializeObject(value);
}
}
Set Object
HttpContext.Session.SetObjectInSession("SessionName", ClassObject);
Get Object
HttpContext.Session.GetCustomObjectFromSession("SessionName");
Data Annotations
Data Annotations are used for Validating User Input.
Namespace : System.ComponentModel.DataAnnotations
-
Required For Mandatory fields
[Required(ErrorMessage = "ID is Required")]
public string ID { get; set; }
-
DisplayName For Fields Name
[DisplayName("Enter Your Name: ")]
public string Name { get; set; }
-
StringLength for specifying Minimum and Maximum length of string
[StringLength(50, MinimumLength = 5)]
public string fullName { get; set; }
-
Range For specifying Range in a numeric value
[Range(18,60, ErrorMessage ="Age must be between 18-60 in years.")]
public int Age { get; set; }
-
Bind For Binding to Model Property(Include/Exclude)
[Bind(Exclude = "Id")]
-
ScaffoldColumn For hiding from editor forms
[ScaffoldColumn(false)]
public int Id { get; set; }
-
DisplayFormat To specifies a display format for a field like Date , Currency etc
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy hh:mm:ss tt}")]
public System.DateTime? HireDate { get; set; }
-
ReadOnly To set a field to read-only.
[ReadOnly(true)]
public string Name { get; private set; }
-
MaxLength To Specify maximum length of a string
[MaxLength(50)]
public string Name { get; set; }
-
Compare To compare with other fields
[Compare("Password", ErrorMessage = "Password doesn't Match")]
public string ConfirmPassword {get; set;}
-
RegularExpression To specifies that input field is matched with desired Regular Expression.
[RegularExpression(@"[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}", ErrorMessage = "Incorrect Email ID")]
public string EmailID { get; set; }
-
HiddenInput To specifies a hidden input field.
[System.Web.Mvc.HiddenInput(DisplayValue = false)]
public string Name { get; set; }
-
DataType
To provides a list of data type that is associated with input field and parameter.
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
-
Timestamp
-
ConcurrencyCheck
Namespace : System.ComponentModel.DataAnnotations.Schema
-
Table
-
Column
-
Index
-
ForeignKey
-
NotMapped
-
InverseProperty