Permalink
Browse files

Add missing Razor APIs

  • Loading branch information...
mythz committed Dec 24, 2018
1 parent 7328ed4 commit 6d36a6921a6e8e417ef29c752b5c2b22d9058f7d
@@ -468,6 +468,8 @@ public IHttpRequest Request
}
}

public IHttpResponse Response => (IHttpResponse)Request.Response;

public string GetLayout(string defaultLayout) => ViewData["Layout"] as string ?? defaultLayout;

public bool IsError => ModelError != null || GetErrorStatus() != null;
@@ -498,6 +500,24 @@ private static ResponseStatus GetResponseStatus(object response)
return propertyInfo?.GetProperty(response) as ResponseStatus;
}

public HtmlString GetErrorMessage()
{
var errorStatus = GetErrorStatus();
return errorStatus == null ? null : new HtmlString(errorStatus.Message);
}

public HtmlString GetAbsoluteUrl(string virtualPath)
{
return new HtmlString(AppHost.ResolveAbsoluteUrl(virtualPath, Request));
}

public void ApplyRequestFilters(object requestDto)
{
HostContext.ApplyRequestFiltersAsync(Request, Response, requestDto).Wait();
if (Response.IsClosed)
throw new StopExecutionException();
}

public HtmlString GetErrorHtml()
{
return new HtmlString(RazorViewExtensions.GetErrorHtml(GetErrorStatus()) ?? "");
@@ -564,6 +584,50 @@ public void Dispose()
}

public virtual void EndServiceStackRequest() => HostContext.AppHost.OnEndRequest(Request);

public void RedirectIfNotAuthenticated(string redirectUrl = null)
{
if (IsAuthenticated)
return;

redirectUrl = redirectUrl
?? AuthenticateService.HtmlRedirect
?? HostContext.Config.DefaultRedirectPath
?? HostContext.Config.WebHostUrl
?? "/";
AuthenticateAttribute.DoHtmlRedirect(redirectUrl, Request, Response, includeRedirectParam: true);
throw new StopExecutionException();
}

public bool RenderErrorIfAny()
{
var html = GetErrorHtml(GetErrorStatus());
if (html == null)
return false;

WriteLiteral(html);

return true;
}

private string GetErrorHtml(ResponseStatus responseStatus)
{
if (responseStatus == null) return null;

var stackTrace = responseStatus.StackTrace != null
? "<pre>" + responseStatus.StackTrace + "</pre>"
: "";

var html = @"
<div id=""error-response"" class=""alert alert-danger"">
<h4>" +
responseStatus.ErrorCode + ": " +
responseStatus.Message + @"
</h4>" +
stackTrace +
"</div>";
return html;
}
}
}

@@ -0,0 +1,27 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:52770/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"CheckMvcIdentityCore": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:52775/"
}
}
}
@@ -17,6 +17,7 @@
<ProjectReference Include="..\..\src\ServiceStack.Client\ServiceStack.Client.csproj" />
<ProjectReference Include="..\..\src\ServiceStack.Common\ServiceStack.Common.csproj" />
<ProjectReference Include="..\..\src\ServiceStack.Interfaces\ServiceStack.Interfaces.csproj" />
<ProjectReference Include="..\..\src\ServiceStack.Mvc\ServiceStack.Mvc.csproj" />
<ProjectReference Include="..\..\src\ServiceStack\ServiceStack.csproj" />
<PackageReference Include="ServiceStack.Text" Version="$(Version)" />
</ItemGroup>
@@ -1,18 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Funq;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ServiceStack;
using ServiceStack.Templates;
using ServiceStack.Auth;
using ServiceStack.Caching;
using ServiceStack.DataAnnotations;
using ServiceStack.Mvc;

namespace CheckWebCore
{
@@ -23,7 +19,10 @@ public class Startup

// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services) { }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
@@ -51,6 +50,13 @@ public override void Configure(Container container)
{
Plugins.Add(new TemplatePagesFeature()); // enable server-side rendering, see: http://templates.servicestack.net

if (Config.DebugMode)
{
Plugins.Add(new HotReloadFeature());
}

Plugins.Add(new RazorFormat()); // enable ServiceStack.Razor

SetConfig(new HostConfig
{
AddRedirectParamsToQueryString = true,
@@ -96,8 +102,11 @@ public class HelloResponse
public string Result { get; set; }
}

[Route("/testauth")]
public class TestAuth : IReturn<TestAuth> {}


// [Authenticate]
// [Authenticate]
public class MyServices : Service
{
//Return index.html for unmatched requests so routing is handled on client
@@ -109,5 +118,7 @@ public object Any(Hello request)
{
return new HelloResponse { Result = $"Hello, {request.Name}!" };
}

public object Any(TestAuth request) => request;
}
}
@@ -0,0 +1,5 @@
@model HelloResponse

<p>View Page for <a href="/hello?Name=World">/hello</a> API</p>

<h1>@Model.Result</h1>
@@ -0,0 +1,11 @@
@{
var menuItems = base.AppSettings.GetDictionary("Menu");
}

<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
@foreach (var item in menuItems) {
<li class="nav-item @(item.Key == Request.PathInfo ? "active" : "")"><a class="nav-link" href="@item.Key">@item.Value</a></li>
}
</ul>
</div>
@@ -0,0 +1,55 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>@ViewBag.Title</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">

<link rel="stylesheet" href="/assets/css/bootstrap.css">
<link rel="stylesheet" href="/assets/css/default.css">
<link rel="icon" type="image/x-icon" href="/assets/img/favicon.ico">
</head>
<body>
@if (DebugMode) {
<script>@Html.IncludeFile("js/hot-fileloader.js")</script>
}

<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
<div class="container">
<a title="ServiceStack WebApp Starter" class="navbar-brand" href="/" style="width:220px;">
<img src="/assets/img/logo.png" width="42" height="32" class="d-inline-block align-top" alt="" style="float:left;margin:0 10px 0 0">
<h3>MyApp</h3>
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
@await Html.PartialAsync("MenuPartial")
</div>
</nav>

<!-- Page Content -->
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h1 class="mt-5">@ViewBag.Title</h1>
@RenderBody()
</div>
</div>
</div>

<h4 style="position:absolute; bottom: 20px; width: 100%; text-align: center">
<a href="http://razor.netcore.io">Learn about ServiceStack Razor</a>
</h4>

<script src="/assets/js/jquery.min.js"></script>
<script src="/assets/js/bootstrap.min.js"></script>
<script src="/assets/js/default.js"></script>

@RenderSection("scripts", required: false)

</body>
</html>
@@ -0,0 +1,5 @@
@model TestAuth

@{ RedirectIfNotAuthenticated(); }

<p>View Page for <a href="/testauth">/testauth</a> API</p>
@@ -0,0 +1,8 @@
@inherits ViewPage<TModel>

@using ServiceStack
@using ServiceStack.Mvc
@using ServiceStack.Text
@using CheckWebCore

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@@ -0,0 +1,21 @@
{
"Menu": {
"/": "Home",
"/about": "About",
"/services": "Services",
"/contact": "Contact"
},
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
}
}

0 comments on commit 6d36a69

Please sign in to comment.