Search code examples
c#razorservicestackservicestack-bsd

ServiceStack: Self-Host Not finding static files


I have a self hosted service sitting at the following URI:

const string listeningOn = "http://*:80/PassengerTracker/";

Note: I want to host it at /PassengerTracker. Is this the best way to do it?

I have Razor Views enabled and I have specified the URL like so for my CSS static files in my _Layout.cshtml file:

<link href="@Url.Content("~/Content/bootstrap.min.css")" rel="stylesheet" />

However, this is returning a 404. I have set the Copy to Ouput Directory to Copy Always

Edit

If I try:

<link href="@(Request.GetApplicationUrl() 
               + "/Content/bootstrap.min.css")" rel="stylesheet" />

I get a NotImplementedException..

If I change the listeningOn to http://*:80/. I can use @Url.Content fine.

Edit 2

I tried the following as per @Scott comment below:

<link href="@(AppHost.Config.ServiceStackHandlerFactoryPath 
                      + "/Content/bootstrap.min.css")" rel=" stylesheet" />

But it returns a 404:

GET http://localhost:8090/passengertracker/PassengerTracker/Content/bootstrap.min.css 
404

I noticed its putting PassengerTracker twice?


Solution

  • From the chat discussion, there seems to be an issue with your ServiceStack's static file handler, so you could create a simple service that essentially performs the same task and serves the files from the Content directory.

    [Route("/content/{filePath*}", "GET")] 
    public class ContentRequest : IReturnVoid 
    { 
        public string filePath { get; set; } 
    } 
    
    public class ContentService : Service 
    { 
        public void Get(ContentRequest request) 
        { 
            var fullPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "Content", request.filePath); 
            Response.WriteFile(fullPath); 
            Response.End(); 
        } 
    }
    

    This should work for you, I hope :)