Reputation:
I am working with ASP.Net MVC. When I keep the css inside the Views folder, its not loading the CSS. Can anybody help me regarding this issue?
Upvotes: 3
Views: 2536
Reputation: 7415
Allow those files to be served by adding static file handlers to the web.config in the Views folder:
<system.webServer>
<handlers>
<remove name="BlockViewHandler"/>
<add name="JsHandler" path="*.js" verb="*" type="System.Web.StaticFileHandler" />
<add name="MapHandler" path="*.js.map" verb="*" type="System.Web.StaticFileHandler" />
<add name="TsHandler" path="*.ts" verb="*" type="System.Web.StaticFileHandler" />
<add name="CssHandler" path="*.css" verb="*" type="System.Web.StaticFileHandler" />
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
Upvotes: 1
Reputation: 181
I like a css file per View folder, just for organization. That way I can work with html and the corresponding css in the same directory instead of having a big css file.
Here is how I do it:
First made a "Style controller" which is going to merge all files into one:
public class StyleController : Controller
{
[OutputCache(Duration = 86400, VaryByParam = "none")]
public ActionResult Index()
{
var cssFiles = Directory.GetFiles(Server.MapPath("~/Views"), "*.css",
SearchOption.AllDirectories);
var sb = new StringBuilder();
foreach (var cssFile in cssFiles)
{
sb.Append(System.IO.File.ReadAllText(cssFile));
sb.Append(Environment.NewLine);
}
return Content(sb.ToString(), "text/css");
}
}
Then in the layout file add the reference to the style file
@Styles.Render("~/Style")
This work great for developing, but in production I rather extract the output of the StyleController into a physical file and use the bundle features (change the line "@Styles.Render")
Upvotes: 2
Reputation:
Thank you so much for the answers. The problem is the web.config inside the Views folder is blocking the CSS from loading. Now the issue has been solved.
Upvotes: 0
Reputation: 9020
If you check the Views\Web.Config you will notice that all HTTP requests will be passed of to the System.Web.HttpNotFoundHandler.
You can of course "hack" your way out of it by modifying this (of course strongly discouraged!), but another issue you might experience is that your "CSS" request could risk being mapped to a controller action (depending on your routes and web server setup) so that you would also have to modify the routes collection on global.asax.cs
All in all: to make you get to where you want you would have to (heavily) modify the way ASP.NET MVC is working/structured which is probably not a good idea...
Upvotes: 1
Reputation: 1062600
Updated: nope, it looks like that is blocked (404); you'll have to keep them outside, it seems. This makes sense, comparable to App_Code blocking, etc.
(original answer) How are you referencing the css in your view/master page? Are you using "/views/foo.css", "~/views/foo.css", "foo.css", or what? Recall that the browser will think that it is in "Some/Invented/Path", so your css resolution needs to be relative to that - "~/" (app-root) being the simplest.
I'm just taking a look now...
Upvotes: 0