Require All Application to use SSL

public class FilterConfig
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        filters.Add(new HandleErrorAttribute());
        filters.Add(new AuthorizeAttribute());

        //require application to use SSL
        filters.Add(new RequireHttpsAttribute());

After that, we need to update project url. Right click on project > properties > web and update Project Url like https://localhost:44331/

Adding Roles in ASP.NET MVC 5

//UserStore & RoleStore
//UserManager, RoleManer and SignInManger

var roleStore = new RoleStore(new ApplicationDbContext());
var roleManager = new RoleManager(roleStore);
roleManager.Create(new IdentityRole("Role1"));
UserManager.AddToRole(user.Id, "Role1");

Note: UserManager.AddToRole(user.Id, "Role1")
will add record to AspNetUserRoles but not the cookie. Later when we check the role using
The above code will return false because it checks the cookie. We need to re-signin the user to update the cookie
SignInManager.SignIn(user, false, false);
Identity information like roles and claims are stored in the cookie when user logs in.

Adding Global Filters to ASP.NET MVC 5

In App_Start > FilterConfig.cs
public class FilterConfig
  public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    filters.Add(new HandleErrorAttribute()); //redirects the user to error page when an actions throws an exception
    filters.Add(new AuthorizeAttribute()); //all controllers will require authentication so we don't have to add [Authorize]

The object ‘DF__Jobs__IsPrivate__52E34C9D’ is dependent on column ‘IsPrivate’.

I have a column IsPrivate which was originally tiny int but I decided to change it to bool

PM> add-migration MigrationName results to

AlterColumn("dbo.Jobs", "IsPrivate", c => c.Boolean(nullable: false));

When I run update-database, I got an error
The object 'DF__Jobs__IsPrivate__52E34C9D' is dependent on column 'IsPrivate'.
ALTER TABLE ALTER COLUMN IsPrivate failed because one or more objects access this column.

It’s because IsPrivate column has default constraint DF__Jobs__IsPrivate__52E34C9D so I need to manually add code to drop it
Sql("ALTER TABLE dbo.Jobs DROP CONSTRAINT DF__Jobs__IsPrivate__52E34C9D");
AlterColumn("dbo.Jobs", "IsPrivate", c => c.Boolean(nullable: false));

When I run update-database again, this time it’s successful

Getting Current Controller and Action Name in Controller or View

Getting in View current controller, action and id in View
string controller = ViewContext.RouteData.Values["controller"].ToString();
string action = ViewContext.RouteData.Values["action"].ToString();

Getting current controller, action and id in Controller
string actionName = this.ControllerContext.RouteData.Values["action"].ToString();
string controllerName = this.ControllerContext.RouteData.Values["controller"].ToString();

Add Auto Increment Key To Table

public class Company
    public int Id { get; set; }

    [Key, Column(TypeName = "varchar"), MaxLength(50)]
    public string UserId { get; set; }

public class CompanyConfiguration : EntityTypeConfiguration<Company>
  public CompanyConfiguration()
      //for query purpose only
      Property(c => c.Id)

      //this is the Key to One-to-One/Zero relationship
      HasKey(c => c.UserId);
} MVC Custom Error Pages

In web.config > system.web
modes: On, Off, RemoteOnly
<!--On - display Views/Shared/Error.cshtml only rendered when there is an exception, 404 is not included in this custom error -->
<customErrors mode="RemoteOnly"></customErrors>

This applies whenever the server hits 500 or 404 errors. Not good in debugging but good in production. In system.webServer

<!--errorMode="DetailedLocalOnly" display only to remote clients-->
    <!--errorMode="Custom" display to all both local and remote -->
    <httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="404" subStatusCode="-1" />
      <remove statusCode="500" subStatusCode="-1" />
      <error statusCode="404" path="404.html" responseMode="File" />
      <error statusCode="500" path="500.html" responseMode="File" />

Note: adding the second block will override the first block

Adding Build Configuratior in Asp.Net

Beside “Debug” there is a dropdown, click and select “Configuration Manager”. In “Active Solution Configuration” select “New” then add a name like “Testing” and “Select copy settings from” preferrably copy settings from Release.

Right click “web.config” and select “Add Config Transform”. This will create Web.Testing.config.