How to serve android application from ASP.NET Core MVC

Is it possible to use this URL domain.tld/android_application_name.apk to serve an Android application apk file from AndroidApplications directory?

In other words, I have a folder sibling to Controllers folder and wwwroot folder and Program.cs file. It's called AndroidApplications and inside it I have a bunch of .apk files. But I don't want to add an extra segment to the URL for them. I want to be able to serve app1.apk via this URL: domain.tld/app1.apk. How can I achieve that?

Upvotes: 1

Views: 672

Answers (2)

Marv
Marv

Reputation: 138

I've seen a few answers like: 1) add ".apk" extension and correspoding mime type in IIS

enter image description here

2) add below node in system.webServer node of web.config

But neither works.

So I found another solution and it works for me

3) set ServeUnknowFileType of StaticFileOptions to true in the Configure method of Startup

var staticFileOptions = new StaticFileOptions { ServeUnknownFileTypes = true };

        app.UseStaticFiles(staticFileOptions);

But I'm worried about the security issue.

Also the above two solutions might be for windows only, it will still be an issue when migrated to linux, correct me if I'm wrong.

Upvotes: 1

Tseng
Tseng

Reputation: 64170

You should have a look at the Url Rewriting Middleware.

public void Configure(IApplicationBuilder app)
{
    var options = new RewriteOptions()
        .AddRewrite(@"^(.*?\.apk)$", "AndroidApplications/$1", 
            skipRemainingRules: true)

    app.UseRewriter(options);
}

This should rewrite all urls ending with apk, i.e. from /myapp.apk urls to /AndroidApplications/myapp.apk.

Upvotes: 1

Related Questions