1

I am trying to make something like "templates system" it might allow user to change site template. In admin panel for example.

My current files structure

-Controllers
-Models
-Views
--Templates
---DefaultTemplate
----css
-----style.css
----Index.cshtml

In default controller (home) i check current template name and show right view

private ViewResult TemplateView(string viewName)
{
    return View($"~/Views/Templates/{GlobalSettings.CurrentTemplate}/{viewName}.cshtml");
}

And some template View (Index.cshtml)

.....
<link rel="stylesheet" href="~/Views/Templates/DefaultTemplate/css/style.css" type="text/css" />
.....

But I dont know how write correct path to my css\js\ images in right template forder.

I have tried

./css/style.css

@Url.Content("/css/style.css")

But in result HTML it is like mysite.com/css/style.css so i got 404 (not found)

I have also tried this

    ~/Views/Templates/DefaultTemplate/css/style.css

In this case i got 404 too.

So, can anybody help me? How to write correct URL to CSS, images, hs etc ?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Stas Petrov
  • 315
  • 5
  • 17

1 Answers1

1

The web.config file in the /Views folder restricts all access to files in the folder by default:

Add code to your web.config file:

<httpHandlers>
    <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>

Note: It's not good practice to store css files inside view folder.

Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
  • Can i allow acces only for specified path? Not for all views folder? – Stas Petrov Sep 20 '16 at 12:10
  • refer [this](http://stackoverflow.com/questions/604883/where-to-put-view-specific-javascript-files-in-an-asp-net-mvc-application), but why you stored inside view folder? – Divyang Desai Sep 20 '16 at 12:23
  • User -friendliness. "Just put folder with views,css,js,images in templates folder and chose it in settings" - no need to put styles in "css/template", stripts in "js/template" etc. – Stas Petrov Sep 20 '16 at 12:29