New Custom Post Type is not working

-rw-r--r-- 1 user members 842 3月 8 18:21 single-about.php
-rw-r--r-- 1 user members 842 3月 8 18:36 single-course.php

I already have an existing custom post type “about”. It’s loaded using single-about.php
I added new custom post type “course” and copied single-about.php content to single-course.php but the new post under “course” custom post type cannot be viewed.
And there’s also no sign that single-course.php is used.

The solution was to flush rewrite, you can do this by going to Settings > Permalinks and just hit Save Changes and everything works as expected after that.

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
User.IsInRole("Role1")
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]
  }
}

Sublime Text 3: Adding Auto-Fortmat Hotkey

In Sublime Text menu, Preferences > Key Bindings – User add the code below inside []
{"keys": ["alt+shift+f"], "command": "reindent", "args": {"single_line": false}}
Re-open Sublime Text editor and press ALT+SHIFT+F to format texts

Keep Linux Command Running Even After Closing Shell

Normally when you run linux command in shell, commands will stop once your close the terminal.
To keep it running, use nohup. The & will make it run in background.

$ nohup command &
$ disown

If the command will listen on certain port, check if it’s listening
netstat -ap | grep "LISTEN"

tcp 0 0 *:ssh *:* LISTEN -
tcp 0 0 *:9090 *:* LISTEN 22018/php
tcp 0 0 *:9191 *:* LISTEN 22023/php
tcp 0 0 localhost:mysql *:* LISTEN -
tcp6 0 0 [::]:http [::]:* LISTEN -
tcp6 0 0 [::]:ssh [::]:* LISTEN -

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
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    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)
          .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

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

Asp.net 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" />
    </httpErrors>

Note: adding the second block will override the first block