简体   繁体   中英

How to serve static content from Azure Mobile Service C# backend

I'm running a mobile services instance ( C# backend ) and besides all the other functionality, I also need to host a few html pages and their respective css files.

Here is what I tried:

  1. If I browse to http://<service-url>/myfile.html I get an 404 error. So I
    created a myfile.html file and stuck it in the project root folder. Now, instead of 404, I get a blank page.
  2. I tried going through the ContentController by putting the html file in <project root>\\api\\Content\\Views\\myfile.html . When I browse to http://<service-url>/api/Content/Views/myfile.html I get a blank page.

Update Dec, 2015 Now that Microsoft published App Services you should probably use that - create a mobile app and a web app and you're done. Don't try to hack around with Mobile Services. It always felt to me that Mobile Services were a half baked solution. It is more a competitor to Parse.com and similar BaaS and was missing a lot of the stuff you'd expect from an Azure service.

Update The solution below does not work but it does teach you a lot about how the mobile service is built. The reason the solution does not work is that Azure refuses to upload the static files to the mobile service instance.

You start by understanding what is Owin and that mobile services are using one. This post helped me a lot.

Then you look in this link which explains how to build a file server using katana.

Here are short instructions:

Install nuget package Microsoft.Owin.StaticFiles .

Plug into the OwinAppBuilder and instruct it to use Katana's file server just before you run the original (put this in WebApiConfig.cs ):

var originalAppBuilder = StartupOwinAppBuilder.OwinAppBuilder;
StartupOwinAppBuilder.Initialize(builder =>
{
    builder.UseFileServer("/static");
    originalAppBuilder(builder);
}

Create a static folder in your project root, put some files there and browse to <your service url>/static/somefile.html .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM