Stas Petrov Stas Petrov - 2 months ago 23
ASP.NET (C#) Question

ASP.NET MVC css in different folder

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

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 ?

Div Div
Answer

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.

Comments