css file not found - asp.net core web application - asp.net-core-mvc

I have created an asp.net core web application (I started with the Empty option) and I am building it step by step. It seems my css file is not being read or found though.
When I launch the application I can see that my html page does not look as it should be and when I use the developer tools in edge under the console there is an HTTP404: NOT FOUND - The server has not found anything matching the requested URI (Uniform Resource Identifier). GET - http://localhost:8888/css/site.css error which would indicate my css file is indeed not being read or found.
Now, my css file is under a folder called "css" which is in the root of the project. When I say root I mean that I have just created a folder called css and the path is the same of Program.cs or Startup.cs. There is no wwwroot in my project (seems that asp.net core 2.2 has removed it by default?) and the css folder is not there. As follows the code from my html page, I have tried to add the ~ before the "/css" but still nothing.
<head>
<link href="/css/site.css" rel="stylesheet" />
</head>

If you would like that static files to be served outside of the web root (for example in a folder named MyStaticFiles) and it has the structure like
-YourProject
--MyStaticFiles
---css
----site.css
--Program.cs
--Startup.cs
You could use app.UseStaticFiles, refer to Serve files outside of web root
public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "MyStaticFiles")),
RequestPath = "/StaticFiles"
});
}
Then you could get the css from http://localhost:8888/StaticFiles/css/site.css
or <link rel="stylesheet" href="~/StaticFiles/css/site.css" />

It is happening because environment webroot is different than the css folder is currently in. You have to change the webroot or move your css folder to current webroot.
Normally the wwwroot folder is set at environment webroot

Related

Codeigniter 4 assets PATH configuration for Modules

I m creating different directories for Role-based access in Codeigniter 4. I have created the Modules directory inside app/ i.e (app/modules/admin). I have assets for the admin. I want to load it in the header file of view. I have tried it with the base_url().'/app/modules/admin/assets/filename.xyz' but load the complete file in the link but doesn't show me the assets. How can, I load these assets in the same directory. Please see the below code
<link rel="stylesheet" href="<?php echo(base_url().'/app/modules/super/assets/'); ?>plugins/fontawesome-free/css/all.min.css">
All public files including assets should be placed inside your public folder and not your app folder. Your app folder shouldn't be visible to the public.
With this in mind what I would do would be create a simular structure in your public folder.
public/modules/admin/css
public/modules/admin/js
public/modules/admin/img
Then if you have an asset called styles that would be in:
public/modules/admin/css/styles.css
This asset should be called using the base_url function from the uri helper.
<link src="<?php echo base_url('modules/admin/css/styles.css'); ?>">

Including page layout to Laravel project

I was supplied with a custom layout for the login page of my Laravel project.
When I opened login.html file, which represents the layout for that specific page I saw such links
<!-- Base Css Files -->
<link href="assets/libs/jqueryui/ui-lightness/jquery-ui-1.10.4.custom.min.css" rel="stylesheet" />
<link href="assets/libs/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
<link href="assets/libs/font-awesome/css/font-awesome.min.css" rel="stylesheet" />
...
So I figured that I only need to copy the assets folder, which came with the template (there are all needed bootstraps, jqueries and whatnot) to my projects app\resources\assets directory
Now, when I copied the code from login.html into my login.blade.php view and copied the templates' assets folder to app\resources\assets it doesnt work. It only displays naked html code when I open the page.
What am I doing wrong in linking the assets folder?
The resources folder is (like the name says) for the resources.
If you don't want/need to build/compile/min your scripts, then just put them in the public folder, so you can access them from your template.
In your case
public/assets/libs...
In order to access assets, you have two ways to approach it. Either using Elixir/gulp or to use direct access.
Gulp is a node.js application that reads your assets files, whether JS, CSS, Coffee, etc...and combine them in single files. Gulp reads the files defined in the gulpfile.js, and you can access the output files in your blade file using elixir().
You can read more about Elixir here.
In your specific case, you can just place the files under the public/ directory. So Laravel treats public as the root directory, and if you want to read the file assets/css/foo.css just place the file in public/assets/css/foo.css

keeping safe assets folder in codeigniter

Hi i have this assets folder which has the following folder inside of css, images, uploads, and js folder i put the assets folder outside application. Im wondering that how could i keep it safe so that when users tried to type in the url like for example http:/test.com/assets it will redirect to application folder or somewhat it will says page not found. Cause ive noticed that when i type in the url http://test.com/assets it will go to the assets folder and which is vulnerable and people could see all the folders in the assets folder. can someone help me figued this thing out? Any help is muchly appreciated.
if you want to secure you folder then do one this create html file like this
<html>
<head>
<title>403 Forbidden</title>
</head>
<body>
<p>Directory access is forbidden.</p>
</body>
</html>
and save it as index.html and put it on your assets folder whenever some try to access your url http://test.com/assets then this index file will execute and its shows Directory access is forbidden.

Why would images and script file paths not resolve after deployment in an Asp.Net MVC application

I have developed an Asp.Net MVC application in Visual Studio 2010. So far so good. In terms of path resolution for images and script execution, everything runs perfect within Visual Studio. As I am using the default template of MVC I am using "../../Script/*.js" kind of path. After I build the package for deployment and deploy in IIS, the images and the script references stop working. I think it might be a path resolution issue. I tried with "~/Script/.js" but it does not work either. Could somebody suggest why this is happening?
Consider using two things in any of your asp.net MVC projects.
1) Using a Layout for common site elements using _Layout.cshtml and include all your javascript and css files inside that and do not put it on the any other .cshtml pages unless necessary required. see here how to use _Layout.cshtml
2) Second thing always use #Url.Content() directive to define your paths e.g
<link href="#Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="#Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
This two way always helps you and never have problem even if you deploy package.
Hope above helps.
This type of problem we get in asp.net when we use URL Rewriting and asp.net mvc have a URL Rewriting by default.
so when you use script or urls and js then you set your path like this .
#Url.Content("~/Your Url or Your script or Your js File ") ;
i think this will help you.
MVC has built in functions that generates url's, so use those instead of hard code url
#Url.Content("~/yourpath")
When you deploy your webapp onto IIS, your site is set to run as Virtual Directory so the website root is no longer the same as the application root, MVC has
ActionLinks,Url.Contents to resolve Url's.
MSDN help

Using Virtual Paths in ASP.NET MVC 3

I have a layout page which is shared across all of the views in my app. That view references the .css in my app. For the sake of reference, I currently have the following at the top of my _layout.cshtml file:
<link rel="stylesheet" href="/css/themes/default/app.css" />
I am planning on taking this app and loading into a phone app via PhoneGap. Because of that, I cannot use /css/themes/default.app.css. Instead, I need to get it converted to a string that is relative to the path of the file. For instance, I need to dynamically generate a value that looks something like the following:
<link rel="stylesheet" href="../../../css/themes/default/app.css" />
The number of "../" will be determined based on how deep it is. I figured there would be a utility or something built into the ASP.NET MVC 3 framework to do this. However, I can't find anything. I have a LOT of files and I don't want to have to manually update all of the url patterns. Is there a utility that will automatically handle what I'm trying to acomplish? If so, how?
Thank you
Use the UrlHelper.Content HTML Helper method .
This method Converts a virtual (relative) path to an application absolute path.
<link href="#Url.Content("~/css/themes/default/app.css")"
rel="stylesheet" type="text/css" />
You may drop your css folder under the Content directory in the root of your MVC project

Resources