Skip to content

Commit

Permalink
fixes Layout.cshtml razor templating for identity. (#1641)
Browse files Browse the repository at this point in the history
  • Loading branch information
deepchoudhery committed Sep 14, 2021
1 parent 106f359 commit 631ea07
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ private string GetBuilderIdentifier(MemberDeclarationSyntax builderMember)
private string AddDbContextString(bool minimalHostingTemplate, bool useSqlite, string statementLeadingTrivia)
{
string textToAddAtEnd;
string additionalNewline = minimalHostingTemplate ? string.Empty : Environment.NewLine;
string additionalNewline = Environment.NewLine;
string additionalLeadingTrivia = minimalHostingTemplate ? string.Empty : " ";
string leadingTrivia = minimalHostingTemplate ? string.Empty : statementLeadingTrivia;
if (useSqlite)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
"CodeChanges": [
{
"InsertAfter": "WebApplication.CreateBuilder",
"CheckBlock": "builder.Configuration.GetConnectionString",
"Block": "\nvar connectionString = builder.Configuration.GetConnectionString(\"{0}\");"
},
{
"InsertAfter": "builder.Configuration.GetConnectionString",
"CheckBlock": "builder.Services.AddDbContext",
"Block": "builder.Services.AddDbContext<{0}>(options =>\r\n options.{0}(connectionString));\""
},
{
"InsertAfter": "builder.Services.AddDbContext",
"CheckBlock": "builder.Services.AddDefaultIdentity",
"Block": "builder.Services.AddDefaultIdentity<{0}>(options => options.SignIn.RequireConfirmedAccount = true)\r\n .AddEntityFrameworkStores<{0}>();\""
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
@using Microsoft.AspNetCore.Hosting
@using Microsoft.AspNetCore.Mvc.ViewEngines
@inject IWebHostEnvironment Environment
@inject ICompositeViewEngine Engine
@inherits Microsoft.VisualStudio.Web.CodeGeneration.Templating.RazorTemplateBase
@{
var currentYear = System.DateTime.Now.Year;
}
@{
@:@@using Microsoft.AspNetCore.Hosting
@:@@using Microsoft.AspNetCore.Mvc.ViewEngines
@:@@inject IWebHostEnvironment Environment
@:@@inject ICompositeViewEngine Engine
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - @Environment.ApplicationName</title>
<title>@@ViewData["Title"] - @Model.ApplicationName</title>

<environment include="Development">
<link rel="stylesheet" href="~/Identity/lib/bootstrap/dist/css/bootstrap.css" />
Expand All @@ -25,16 +31,16 @@
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container">
<a class="navbar-brand" href="~/">@Environment.ApplicationName</a>
<a class="navbar-brand" href="~/">@Model.ApplicationName</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
@{
@@{
var result = Engine.FindView(ViewContext, "_LoginPartial", isMainPage: false);
}
@if (result.Success)
@@if (result.Success)
{
await Html.RenderPartialAsync("_LoginPartial");
}
Expand All @@ -52,18 +58,17 @@
<div class="container">
<partial name="_CookieConsentPartial" optional />
<main role="main" class="pb-1">
@RenderBody()
@@RenderBody()
</main>
</div>
<footer class="footer border-top pl-3 text-muted">
<div class="container">
&copy; @DateTime.Now.Year - @Environment.ApplicationName
@{
&copy; @DateTime.Now.Year - @Model.ApplicationName
@@{
var foundPrivacy = Url.Page("/Privacy", new { area = "" });
}
@if (foundPrivacy != null)
@@if (foundPrivacy != null)
{
@:-
<a asp-area="" asp-page="/Privacy">Privacy</a>
}
</div>
Expand All @@ -90,6 +95,6 @@
<script src="~/Identity/js/site.js" asp-append-version="true"></script>
</environment>

@await RenderSectionAsync("Scripts", required: false)
@@await RenderSectionAsync("Scripts", required: false)
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class CodeSnippet
{
public string InsertAfter { get; set; }
public string Block { get; set; }
public string CheckBlock { get; set; }
public string Parent { get; set; }
public string Type { get; set; }
public bool Append { get; set; } = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,9 @@ internal static CompilationUnitSyntax AddGlobalStatements(CodeSnippet change, Co
var newRoot = root;
//create syntax expression that adds DbContext
var expression = SyntaxFactory.ParseStatement(change.Block);
var checkBlock = change.CheckBlock;
var globalStatement = SyntaxFactory.GlobalStatement(expression).WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed);
if (!GlobalStatementExists(newRoot, globalStatement))
if (!GlobalStatementExists(newRoot, globalStatement, checkBlock))
{
//insert after, before, or at the end of file.
//insert global statement after particular statement
Expand Down Expand Up @@ -495,11 +496,17 @@ internal string FormatCodeBlock(string codeBlock, IDictionary<string, string> pa
return formattedCodeBlock;
}

internal static bool GlobalStatementExists(CompilationUnitSyntax root, GlobalStatementSyntax statement)
internal static bool GlobalStatementExists(CompilationUnitSyntax root, GlobalStatementSyntax statement, string checkBlock = null)
{
if (root != null)
if (root != null && statement != null)
{
return root.Members.Where(st => st.ToString().Trim(CodeSnippetTrimChars).Equals(statement.ToString().Trim(CodeSnippetTrimChars))).Any();
bool foundStatement = root.Members.Where(st => st.ToString().Trim(CodeSnippetTrimChars).Equals(statement.ToString().Trim(CodeSnippetTrimChars))).Any();
//if statement is not found due to our own mofications, check for a CheckBlock snippet
if (!string.IsNullOrEmpty(checkBlock) && !foundStatement)
{
foundStatement = root.Members.Where(st => st.ToString().Trim(CodeSnippetTrimChars).Contains(checkBlock.ToString().Trim(CodeSnippetTrimChars))).Any();
}
return foundStatement;
}
return false;
}
Expand Down

0 comments on commit 631ea07

Please sign in to comment.