Skip to content

Instantly share code, notes, and snippets.

@tomasherceg
Last active September 28, 2021 11:43
Show Gist options
  • Save tomasherceg/be18a56ddd1d4974bbb09d0b92bb3710 to your computer and use it in GitHub Desktop.
Save tomasherceg/be18a56ddd1d4974bbb09d0b92bb3710 to your computer and use it in GitHub Desktop.
Install-Package Microsoft.Owin.Host.SystemWeb
Install-Package DotVVM.Owin
using DotVVM.Framework.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WingtipToys.DotvvmSite
{
public class DotvvmStartup : IDotvvmStartup, IDotvvmServiceConfigurator
{
public void Configure(DotvvmConfiguration config, string applicationPath)
{
ConfigureRoutes(config, applicationPath);
ConfigureResources(config, applicationPath);
ConfigureControls(config, applicationPath);
}
private void ConfigureRoutes(DotvvmConfiguration config, string applicationPath)
{
// configure your DotVVM routes here
}
private void ConfigureResources(DotvvmConfiguration config, string applicationPath)
{
}
private void ConfigureControls(DotvvmConfiguration config, string applicationPath)
{
}
public void ConfigureServices(IDotvvmServiceCollection options)
{
}
}
}
using DotVVM.Framework.Configuration;
using Microsoft.Owin;
using Owin;
using System.Web.Hosting;
using WingtipToys.DotvvmSite;
[assembly: OwinStartupAttribute(typeof(WingtipToys.Startup))]
namespace WingtipToys
{
public partial class Startup {
public void Configuration(IAppBuilder app) {
ConfigureAuth(app);
app.UseDotVVM<DotvvmStartup>(HostingEnvironment.ApplicationPhysicalPath,
useErrorPages: HostingEnvironment.IsDevelopmentEnvironment,
debug: HostingEnvironment.IsDevelopmentEnvironment);
}
}
}
@viewModel WingtipToys.DotvvmSite.ViewModels.SiteViewModel, WingtipToys
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title><%: Page.Title %> - Wingtip Toys</title>
<asp:PlaceHolder>
<%: Scripts.Render("~/bundles/modernizr") %>
</asp:PlaceHolder>
<webopt:bundlereference path="~/Content/css" />
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
</head>
<body>
<form>
<asp:ScriptManager>
<Scripts>
<%--To learn more about bundling scripts in ScriptManager see http://go.microsoft.com/fwlink/?LinkID=301884 --%>
<%--Framework Scripts--%>
<asp:ScriptReference Name="MsAjaxBundle" />
<asp:ScriptReference Name="jquery" />
<asp:ScriptReference Name="bootstrap" />
<asp:ScriptReference Name="respond" />
<asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
<asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
<asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
<asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
<asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
<asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
<asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
<asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
<asp:ScriptReference Name="WebFormsBundle" />
<%--Site Scripts--%>
</Scripts>
</asp:ScriptManager>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="~/">Wingtip Toys</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a id="adminLink" visible="false"
href="~/Admin/AdminPage">Admin</a>
</li>
<li><a href="~/">Home</a></li>
<li><a href="~/About">About</a></li>
<li><a href="~/Contact">Contact</a></li>
<li><a href="~/ProductList">Products</a></li>
<li><a href="~/ShoppingCart" ID="cartCount">&nbsp;</a></li>
</ul>
<asp:LoginView ViewStateMode="Disabled">
<AnonymousTemplate>
<ul class="nav navbar-nav navbar-right">
<li><a href="~/Account/Register">Register</a></li>
<li><a href="~/Account/Login">Log in</a></li>
</ul>
</AnonymousTemplate>
<LoggedInTemplate>
<ul class="nav navbar-nav navbar-right">
<li><a href="~/Account/Manage" title="Manage your account">Hello, <%: Context.User.Identity.GetUserName() %> !</a></li>
<li>
<asp:LoginStatus LogoutAction="Redirect" LogoutText="Log off" LogoutPageUrl="~/" OnLoggingOut="Unnamed_LoggingOut" />
</li>
</ul>
</LoggedInTemplate>
</asp:LoginView>
</div>
</div>
</div>
<div id="TitleContent" style="text-align: center">
<a href="~/">
<img ID="Image1" ImageUrl="~/Images/logo.jpg" BorderStyle="None" />
</a>
<br />
</div>
<div id="CategoryMenu" style="text-align: center">
<asp:ListView ID="categoryList"
ItemType="WingtipToys.Models.Category"
SelectMethod="GetCategories">
<ItemTemplate>
<b style="font-size: large; font-style: normal">
<a href="<%#: GetRouteUrl(" ProductsByCategoryRoute", new {categoryName=Item.CategoryName}) %>
">
<%#: Item.CategoryName %>
</a>
</b>
</ItemTemplate>
<ItemSeparatorTemplate> | </ItemSeparatorTemplate>
</asp:ListView>
</div>
<div class="container body-content">
<dot:ContentPlaceHolder ID="MainContent">
</dot:ContentPlaceHolder>
<hr />
<footer>
<p>&copy; <%: DateTime.Now.Year %> - Wingtip Toys</p>
</footer>
</div>
</form>
</body>
</html>
<link rel="stylesheet" type="text/css" href="~/Content/css" />
@viewModel WingtipToys.DotvvmSite.ViewModels.SiteViewModel, WingtipToys
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title><%: Page.Title %> - Wingtip Toys</title>
<link rel="stylesheet" type="text/css" href="~/Content/css" />
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="~/">Wingtip Toys</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a id="adminLink" visible="false"
href="~/Admin/AdminPage">Admin</a>
</li>
<li><a href="~/">Home</a></li>
<li><a href="~/About">About</a></li>
<li><a href="~/Contact">Contact</a></li>
<li><a href="~/ProductList">Products</a></li>
<li><a href="~/ShoppingCart" ID="cartCount">&nbsp;</a></li>
</ul>
<asp:LoginView ViewStateMode="Disabled">
<AnonymousTemplate>
<ul class="nav navbar-nav navbar-right">
<li><a href="~/Account/Register">Register</a></li>
<li><a href="~/Account/Login">Log in</a></li>
</ul>
</AnonymousTemplate>
<LoggedInTemplate>
<ul class="nav navbar-nav navbar-right">
<li><a href="~/Account/Manage" title="Manage your account">Hello, <%: Context.User.Identity.GetUserName() %> !</a></li>
<li>
<asp:LoginStatus LogoutAction="Redirect" LogoutText="Log off" LogoutPageUrl="~/" OnLoggingOut="Unnamed_LoggingOut" />
</li>
</ul>
</LoggedInTemplate>
</asp:LoginView>
</div>
</div>
</div>
<div id="TitleContent" style="text-align: center">
<a href="~/">
<img ID="Image1" ImageUrl="~/Images/logo.jpg" BorderStyle="None" />
</a>
<br />
</div>
<div id="CategoryMenu" style="text-align: center">
<asp:ListView ID="categoryList"
ItemType="WingtipToys.Models.Category"
SelectMethod="GetCategories">
<ItemTemplate>
<b style="font-size: large; font-style: normal">
<a href="<%#: GetRouteUrl(" ProductsByCategoryRoute", new {categoryName=Item.CategoryName}) %>
">
<%#: Item.CategoryName %>
</a>
</b>
</ItemTemplate>
<ItemSeparatorTemplate> | </ItemSeparatorTemplate>
</asp:ListView>
</div>
<div class="container body-content">
<dot:ContentPlaceHolder ID="MainContent">
</dot:ContentPlaceHolder>
<hr />
<footer>
<p>&copy; <%: DateTime.Now.Year %> - Wingtip Toys</p>
</footer>
</div>
</body>
</html>
<dot:AuthenticatedView>
<NotAuthenticatedTemplate>
<ul class="nav navbar-nav navbar-right">
<li><a href="~/Account/Register">Register</a></li>
<li><a href="~/Account/Login">Log in</a></li>
</ul>
</NotAuthenticatedTemplate>
<AuthenticatedTemplate>
<ul class="nav navbar-nav navbar-right">
<li><a href="~/Account/Manage" title="Manage your account">Hello, {{resource: Context.HttpContext.User.Identity.Name}} !</a></li>
<li>
<dot:LinkButton Text="Log off" Click="{command: LogOut()}" />
</li>
</ul>
</AuthenticatedTemplate>
</dot:AuthenticatedView>
<dot:Repeater DataSource="{value: Categories}">
<ItemTemplate>
<b style="font-size: large; font-style: normal">
<a href="{value: "category/" + CategoryName}">
{{value: CategoryName}}
</a>
</b>
</ItemTemplate>
<SeparatorTemplate> | </SeparatorTemplate>
</dot:Repeater>
<title>{{resource: PageTitle}} - Wingtip Toys</title>
<li><a href="~/ShoppingCart" ID="cartCount">Cart ({{value: CartCount}})</a></li>
@viewModel WingtipToys.DotvvmSite.ViewModels.SiteViewModel, WingtipToys
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>{{resource: PageTitle}} - Wingtip Toys</title>
<link rel="stylesheet" type="text/css" href="~/Content/css" />
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="~/">Wingtip Toys</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<dot:RoleView Roles="Administrator">
<a href="~/Admin/AdminPage">Admin</a>
</dot:RoleView>
</li>
<li><a href="~/">Home</a></li>
<li><a href="~/About">About</a></li>
<li><a href="~/Contact">Contact</a></li>
<li><a href="~/ProductList">Products</a></li>
<li><a href="~/ShoppingCart" ID="cartCount">Cart ({{value: CartCount}})</a></li>
</ul>
<dot:AuthenticatedView>
<NotAuthenticatedTemplate>
<ul class="nav navbar-nav navbar-right">
<li><a href="~/Account/Register">Register</a></li>
<li><a href="~/Account/Login">Log in</a></li>
</ul>
</NotAuthenticatedTemplate>
<AuthenticatedTemplate>
<ul class="nav navbar-nav navbar-right">
<li><a href="~/Account/Manage" title="Manage your account">Hello, {{resource: Context.HttpContext.User.Identity.Name}} !</a></li>
<li>
<dot:LinkButton Text="Log off" Click="{command: LogOut()}" />
</li>
</ul>
</AuthenticatedTemplate>
</dot:AuthenticatedView>
</div>
</div>
</div>
<div id="TitleContent" style="text-align: center">
<a href="~/">
<img ID="Image1" src="~/Images/logo.jpg" BorderStyle="None" />
</a>
<br />
</div>
<div id="CategoryMenu" style="text-align: center">
<dot:Repeater DataSource="{value: Categories}">
<ItemTemplate>
<b style="font-size: large; font-style: normal">
<a href="{value: "category/" + CategoryName}">
{{value: CategoryName}}
</a>
</b>
</ItemTemplate>
<SeparatorTemplate> | </SeparatorTemplate>
</dot:Repeater>
</div>
<div class="container body-content">
<dot:ContentPlaceHolder ID="MainContent">
</dot:ContentPlaceHolder>
<hr />
<footer>
<p>&copy; {{resource: DateTime.Now.Year}} - Wingtip Toys</p>
</footer>
</div>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DotVVM.Framework.Hosting;
using DotVVM.Framework.ViewModel;
using WingtipToys.Logic;
using WingtipToys.Models;
namespace WingtipToys.DotvvmSite.ViewModels
{
public abstract class SiteViewModel : DotvvmViewModelBase
{
public int CartCount { get; set; }
public abstract string PageTitle { get; }
[Bind(Direction.ServerToClientFirstRequest)]
public List<CategoryDto> Categories { get; set; }
public void LogOut()
{
Context.GetAuthentication().SignOut();
Context.RedirectToLocalUrl("~/");
}
public override Task PreRender()
{
if (!Context.IsPostBack)
{
using (var usersShoppingCart = new ShoppingCartActions())
{
CartCount = usersShoppingCart.GetCount();
}
Categories = GetCategories();
}
return base.PreRender();
}
private List<CategoryDto> GetCategories()
{
var _db = new WingtipToys.Models.ProductContext();
IQueryable<Category> query = _db.Categories;
return query
.Select(q => new CategoryDto()
{
CategoryName = q.CategoryName
})
.ToList();
}
public class CategoryDto
{
public string CategoryName { get; set; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DotVVM.Framework.ViewModel;
namespace WingtipToys.DotvvmSite.ViewModels
{
public class TestViewModel : SiteViewModel
{
public override string PageTitle => "Test";
}
}
// paste into ConfigureRoutes method
config.RouteTable.Add("Test", "test", "DotvvmSite/Views/Test.dothtml");
using Microsoft.Owin;
using Microsoft.Owin.Extensions;
using Owin;
using System.Web;
using System.Web.Hosting;
using System.Web.SessionState;
using WingtipToys.DotvvmSite;
[assembly: OwinStartupAttribute(typeof(WingtipToys.Startup))]
namespace WingtipToys
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
app.Use((context, next) =>
{
var httpContext = context.Get<HttpContextBase>(typeof(HttpContextBase).FullName);
httpContext.SetSessionStateBehavior(SessionStateBehavior.Required);
return next();
});
app.UseStageMarker(PipelineStage.MapHandler);
ConfigureAuth(app);
app.UseDotVVM<DotvvmStartup>(HostingEnvironment.ApplicationPhysicalPath,
useErrorPages: HostingEnvironment.IsDevelopmentEnvironment, debug: HostingEnvironment.IsDevelopmentEnvironment);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment