Self hosted with razor views checking views for authenticated session

I am going to be making the transition from removing mvc controllers from my project and just use servicestack with razor templates. The only issue is I am use to using filter attributes to prevent a user from being able to get a view document. How would I be able to accomplish this using razor?

You can check base.IsAuthenticated, e.g:

@if (IsAuthenticated)

Example in HttpBenchmarks default.cshtml

This is just a short-hand for checking if the User Session is IsAuthenticated, i.e:

public bool IsAuthenticated
    get { return this.GetSession().IsAuthenticated; }

Im trying to test it out but I keep getting a ‘ServiceStack.Text.JsConfig’ threw an exception.

What’s the StackTrace? and what’s the code causing it?

 public class AppHost : AppSelfHostBase
        /// <summary>
        /// Default constructor.
        /// Base constructor requires a name and assembly to locate web service classes. 
        /// </summary>
        public AppHost()
            : base("SelfHost2", typeof (MyServices).Assembly) // exception occurs here only after trying to register my repository and dbfactory


public override void Configure(Container container)
            //Config examples
            this.Plugins.Add(new PostmanFeature());
            this.Plugins.Add(new CorsFeature());

            this.Plugins.Add(new RazorFormat());
            var dbfactory = (new OrmLiteConnectionFactory(getConnection(), PostgreSqlDialect.Provider));
            var authRepo = new OrmLiteAuthRepository(dbfactory);

            Plugins.Add(new AuthFeature(() => new AuthUserSession(),
                new IAuthProvider[]
                    new CredentialsAuthProvider(),
                HtmlRedirect = "~/",
                IncludeRegistrationService = true,

            SetConfig(new HostConfig
                DebugMode = true,
                WebHostPhysicalPath = Path.GetFullPath(Path.Combine("~".MapServerPath(), "..", "..")),


        private string getConnection()
                ConnectionStringSettings objConnSetting = ConfigurationManager.ConnectionStrings["postgresqltester"];
                return objConnSetting.ConnectionString;

  class Program
        static void Main(string[] args)
            new AppHost().Init().Start("http://*:3465/");
            "ServiceStack Self Host with Razor listening at http://localhost:3465 ".Print();

at ServiceStack.Text.JsConfig.BeginScope()
at ServiceStack.ServiceStackHost…ctor(String serviceName, Assembly[] assembliesWithServices)
at ServiceStack.Host.HttpListener.HttpListenerBase…ctor(String serviceName, Assembly[] assembliesWithServices)
at ServiceStack.AppSelfHostBase…ctor(String serviceName, Assembly[] assembliesWithServices)
at SelfHost2.AppHost…ctor() in c:\users\thekidcunningham\documents\visual studio 2015\Projects\SelfHost2\SelfHost2\SelfHost2\AppHost.cs:line 22
at SelfHost2.Program.Main(String[] args) in c:\users\thekidcunningham\documents\visual studio 2015\Projects\SelfHost2\SelfHost2\SelfHost2\Program.cs:line 14
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

How/where are you registering the license key?

I used every step you gave on the instructions. I have it working in my other project before the app.init in my global asax. But i am not to familiar with the console app, I put it before the app in it in the program start up. program.cs

Which instructions?

The license key is needed when the free-quotas are exceeded, if you haven’t done so it needs to be configured using any one of the license registration options. (The environment variable option is only available from latest v4.0.48 release)

I purchased one last night, and on my account (here at servicestack) there are steps you display on setting up your license key. I have an MVC project that works perfectly fine. I just wanted to make the switch because I see that my controllers are not really being used, so I am looking for an alternate route using your framework only. I don’t know if it makes a difference but I am using your nuget template package for it.

ok but I still don’t know if/how the license key is registered with the project or not? and it needs to be if you’ve exceeded the free quotas.

Can you upload a stand-alone repro of your project (e.g. in a new GitHub repo) so I can debug locally what the issue is - I can’t repro it with the code provided.

sure give me a moment, and i will send you the link

I reproduced the issue but I don’t know exactly why it is happening. Everything runs fine including the license key. The breaking part is the app config for the database connection. As soon as I add the connection string in app settings, the program throws that exception. I remove it, everything works fine.

<add name="postgresqltester" connectionString="Server=;Port=5432;Database=postgres;User Id=postgres;Password=test123;" />

If you’ve registered it in <appSettings/> then you need to fetch it from appSettings, you’re code is currently looking at ConnectionStrings instead, you can change it to:

private string getConnection()
    return ConfigUtils.GetAppSetting("postgresqltester");

I fixed the issues by moving my connection configuration in the App settings all the way down.
A quick question for console applications with razor. I never worked with deploying console apps as a web app, Is it similar to a regular project?

<?xml version="1.0" encoding="utf-8" ?>
--placed here before---
        <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor">
          <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor" requirePermission="false" />
          <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor" requirePermission="false" />
        <add key="webPages:Enabled" value="false" />
        <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc" />
        <pages pageBaseType="ServiceStack.Razor.ViewPage">
            <add namespace="System" />
            <add namespace="ServiceStack" />
            <add namespace="ServiceStack.Html" />
            <add namespace="ServiceStack.Razor" />
            <add namespace="ServiceStack.Text" />
            <add namespace="ServiceStack.OrmLite" />
            <add namespace="SelfHost3" />
            <add namespace="SelfHost3.ServiceModel" />
        <add name="postgresqltester" connectionString="Server=;Port=5432;Database=test;User Id=test;Password=test;" />

This works perfect, thank you so much.