Stig Schmidt Nielsson
Stig Schmidt Nielsson

Reputation: 1687

Serve static single page apps (static html) to only authenticated users with ASP.NET MVC

I am making an ASP.NET MVC site which needs to serve a number of single page apps.

A single page app is static html, css and javascript files a folder with an index.html file and a number of folders for css, images and javascript.

I have no problem serving the single page apps from a virtual directory in an IIS server, but I would like to serve them only to authenticated users in the ASP.NET MVC solution, and therefore I am right now trying serve the index.html files from a controller method.

When I do this by loading and serving the index.html file, then I cannot figure out how to serve all the css and javascript files used by the index.html file because they are referenced by the index.html with relative paths like "css/style.css" and "js/somejavascript.js".

I am using the relative parts to make it easy to develop and debug the single page apps independently from the ASP.NET MVC solution.

So my question is basically, how can I serve static HTML files with relative urls to css and js files to only authenticated users in an ASP.NET MVC site?

Upvotes: 3

Views: 1064

Answers (2)

McGarnagle
McGarnagle

Reputation: 102743

If I'm understanding: you have some static HTML files that you can't/don't want to change, with relative URL link. In that case, just construct your MVC route so that the relative paths work. For example, let's say the relative paths are like this:

js/script.js
css/style.css

When you send that back to the browser, it's going to resolve those paths relative to the current path. So if you're serving the HTML file from the route (say):

Then the browser will send requests to:

So adjust the route accordingly, and make sure any alternate routes redirect to the main one, eg /home/index should redirect to /home.

Upvotes: 1

armen.shimoon
armen.shimoon

Reputation: 6401

Place an

[Authorize]

attribute on the controller or action for serving "index.html".

By default, scripts and css are public, so no authorization is required for them.

Upvotes: -2

Related Questions