Dive into Code

Discover code snippets, tutorials, and programming insights

asp core 3

get active class asp core 3

get active class asp core 3

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public static string IsActive(this IHtmlHelper html,
                                  string control,
                                  string action, string Path)
        {
            string url_link = "/" + control + "/" + action;
            if (action.ToLower() == "index")
            {
                url_link = "/" + control;
            }

            if (url_link == Path)
            {
                return "active";
            }
            return "";
        }


usage  inside html 

                            <a class='nav-link @Html.IsActive("controller" , "method", Context.Request.Path)'  asp-controller="cms" asp-action="index">
                              Dashboard <span class="sr-only">(current)</span>
                            </a>
asp core 3

Rename uploaded file to server

rename uploaded file to server

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public async Task AddFile(Object, IFormFile File)
        {
            if (File != null && File.Length > 0)
            {
                var guid = Guid.NewGuid().ToString();
                guid = guid.Substring(guid.Length - 4);
                string filename = Path.GetFileNameWithoutExtension(File.FileName) + "__" + guid;
                var extension = Path.GetExtension(File.FileName).ToLower();
                var name = $"{filename}{extension}";
                var filePath = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\folder", name);

                // Create new local file and copy contents of uploaded file
                //using (var localFile = System.IO.File.OpenWrite(filePath))
                using (var uploadedFile = new FileStream(filePath, FileMode.Create))
                {
                    await File.CopyToAsync(uploadedFile);
                }
                object.Filepath = name;
            }
        }
MySQL Server

Microsoft sql banners table

Microsoft SQL banners table

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
CREATE TABLE Banners (
    Id int IDENTITY(1,1) not null,
    [Order] int not null default 0,
    Location int not null,
    CreatedAt datetime not null DEFAULT GETDATE(),
    UpdatedAt datetime not null DEFAULT GETDATE(),
    Published bit not null default 0,
    Deleted bit not null default 0,
    BannerImagePath nvarchar(max) NULL,
    BannerCode nvarchar(max) NULL,
    CONSTRAINT PK_Banners_Id PRIMARY KEY CLUSTERED (Id),
);


CREATE TABLE BannersStatistics (
    Id int IDENTITY(1,1) not null,
    SessionId nvarchar(max) null,
    BannersId int not null,
    CreatedAt datetime not null DEFAULT GETDATE(),
    CONSTRAINT PK_Banners_Id PRIMARY KEY CLUSTERED (Id),
    CONSTRAINT FK_BannersStatistics_BannersId FOREIGN KEY (BannersId) REFERENCES  Banners(Id),
);