Link css asp net

How to add js and css files in asp.net core?

In ASP.Net Core, adding JavaScript (JS) and Cascading Style Sheets (CSS) files to your website is an important aspect of web development. These files are used to add interactivity and style to your web pages. However, there are several methods to add these files to your ASP.Net Core website, each with its own advantages and disadvantages. In this article, we will explore the different methods to add JS and CSS files to your ASP.Net Core website.

Method 1: Adding JS and CSS files directly in HTML

To add JS and CSS files directly in HTML in ASP.net Core, you can follow these steps:

  1. Create a new ASP.net Core project or open an existing one.
  2. In the wwwroot folder, create a new folder named «js» and another named «css».
  3. Add your JS and CSS files to their respective folders.
  4. In the HTML file where you want to add the JS and CSS files, add the following code within the head tag:
head> link rel="stylesheet" href="~/css/your-css-file.css"> script src="~/js/your-js-file.js"> script> head>
  1. Replace «your-css-file.css» and «your-js-file.js» with the names of your actual files.
  2. The «~/» in the file paths is used to reference the root directory of the project.
  3. Save the HTML file and run the project.

That’s it! Your JS and CSS files will now be included in the HTML file and can be used throughout the project.

Here’s an example of how to add multiple CSS and JS files:

head> link rel="stylesheet" href="~/css/file1.css"> link rel="stylesheet" href="~/css/file2.css"> script src="~/js/file1.js"> script> script src="~/js/file2.js"> script> head>

You can also use the «asp-append-version» tag helper to append a version number to the file path, which can help with caching:

head> link rel="stylesheet" href="~/css/your-css-file.css" asp-append-version="true"> script src="~/js/your-js-file.js" asp-append-version="true"> script> head>

This will append a version number to the end of the file path, which will change whenever the file is updated, forcing the browser to download the new version.

That’s all there is to it! Adding JS and CSS files directly in HTML in ASP.net Core is a simple process that can greatly enhance the functionality of your project.

Method 2: Using Bundling and Minification

To add js and css files in ASP.net Core using Bundling and Minification, follow these steps:

  1. Install the Microsoft.AspNetCore.Mvc package from NuGet Package Manager.
  2. In the ConfigureServices method of the Startup.cs file, add the following code:
services.AddMvc(); services.AddMvcCore() .AddRazorViewEngine() .AddMvcOptions(options =>  options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute()); >);
app.UseStaticFiles(); app.UseMvc(routes =>  routes.MapRoute( name: "default", template: "//"); >); app.UseMvcWithDefaultRoute();
  1. In the wwwroot folder, create a new folder named lib .
  2. In the lib folder, create two subfolders named css and js .
  3. Copy the css and js files you want to use into their respective folders.
  4. In the Startup.cs file, add the following code to the ConfigureServices method:
services.AddMvc(); services.AddMvcCore() .AddRazorViewEngine() .AddMvcOptions(options =>  options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute()); >); services.AddMvcCore() .AddRazorViewEngine() .AddJsonFormatters() .AddMvcOptions(options =>  options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute()); >); services.AddMvcCore() .AddRazorViewEngine() .AddJsonFormatters() .AddMvcOptions(options =>  options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute()); >) .AddMvcOptions(options =>  options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute()); >) .AddMvcOptions(options =>  options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute()); >); services.AddMvcCore() .AddRazorViewEngine() .AddJsonFormatters() .AddMvcOptions(options =>  options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute()); >) .AddMvcOptions(options =>  options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute()); >) .AddMvcOptions(options =>  options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute()); >) .AddMvcOptions(options =>  options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute()); >);
app.UseStaticFiles(); app.UseMvc(routes =>  routes.MapRoute( name: "default", template: "//"); >); app.UseMvcWithDefaultRoute(); app.UseMvc(routes =>  routes.MapRoute( name: "default", template: "//"); >); app.UseMvcWithDefaultRoute(); app.UseMvc(routes =>  routes.MapRoute( name: "default", template: "//"); >); app.UseMvcWithDefaultRoute(); app.UseMvc(routes =>  routes.MapRoute( name: "default", template: "//"); >); app.UseMvcWithDefaultRoute();
  1. In the Views folder, create a new folder named Shared .
  2. In the Shared folder, create a new file named _Layout.cshtml .
  3. In the _Layout.cshtml file, add the following code:
DOCTYPE html> html> head> meta charset="utf-8" /> meta name="viewport" content="width=device-width, initial-scale=1.0"> title>@ViewData["Title"] - My ASP.NET Applicationtitle> environment include="Development"> link rel="stylesheet" href="~/lib/css/site.css" /> environment> environment exclude="Development"> link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/css/bootstrap.min.css" asp-fallback-href="~/lib/css/bootstrap.min.css" asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" crossorigin="anonymous" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"> environment> link rel="stylesheet" href="~/lib/css/site.css" /> @RenderSection("Styles", required: false) head> body> nav class="navbar navbar-inverse navbar-fixed-top"> div class="container"> div class="navbar-header"> button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> span class="sr-only">Toggle navigationspan> span class="icon-bar">span> span class="icon-bar">span> span class="icon-bar">span> button> a asp-area="" asp-controller="Home" asp-action="Index" class="navbar-brand">My Applicationa> div> div class="navbar-collapse collapse"> ul class="nav navbar-nav"> li>a asp-area="" asp-controller="Home" asp-action="Index">Homea>li> li>a asp-area="" asp-controller="Home" asp-action="About">Abouta>li> li>a asp-area="" asp-controller="Home" asp-action="Contact">Contacta>li> ul> partial name="_LoginPartial" /> div> div> nav> div class="container body-content"> @RenderBody() hr /> footer> p>© 2018 - My ASP.NET Applicationp> footer> div> environment include="Development"> script src="~/lib/js/jquery.js"> script> script src="~/lib/js/bootstrap.js"> script> script src="~/js/site.js" asp-append-version="true"> script> environment> environment exclude="Development"> script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.3.1.min.js" asp-fallback-src="~/lib/js/jquery.min.js" asp-fallback-test="window.jQuery" crossorigin="anonymous" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Piixqu1"> script> environment> script src="~/lib/js/jquery.js">script> script src="~/lib/js/bootstrap.js"> script> script src="~/js/site.js" asp-append-version="true"> script> @RenderSection("Scripts", required: false) body> html>
@section Styles < link rel="stylesheet" href="~/lib/css/your-style.css" /> > @section Scripts < script src="~/lib/js/your-script.js"> script> >

That’s it! Now you can use Bundling and Minification to add css and js files in ASP.net Core.

Method 3: Adding JS and CSS files through Layout or Razor Pages

To add JS and CSS files in ASP.net Core using Layout or Razor Pages, you can follow these steps:

  1. In your ASP.net Core project, create a new folder named «wwwroot» if it doesn’t exist. This folder will hold all the static files in your project.
  2. Inside the «wwwroot» folder, create two new folders named «js» and «css». These folders will hold your JavaScript and CSS files, respectively.
  3. Open the «_Layout.cshtml» file located in the «Views/Shared» folder. This file is the layout page that is shared across all pages in your application.
  4. Add the following code to the head section of the «_Layout.cshtml» file to include the CSS file:
link rel="stylesheet" href="~/css/style.css" />

This code will reference the «style.css» file located in the «wwwroot/css» folder.

  1. Add the following code to the bottom of the body section of the «_Layout.cshtml» file to include the JavaScript file:
script src="~/js/script.js"> script>

This code will reference the «script.js» file located in the «wwwroot/js» folder.

  1. Save the «_Layout.cshtml» file.
  2. Open the Razor Page where you want to use the CSS and JavaScript files.
  3. Add the following code to the head section of the Razor Page to include the CSS file:
link rel="stylesheet" href="~/css/style.css" />

This code will reference the same «style.css» file located in the «wwwroot/css» folder.

  1. Add the following code to the bottom of the body section of the Razor Page to include the JavaScript file:
script src="~/js/script.js"> script>

This code will reference the same «script.js» file located in the «wwwroot/js» folder.

Now, your ASP.net Core application will include the CSS and JavaScript files in the «_Layout.cshtml» file and all Razor Pages that use the «_Layout.cshtml» file.

Method 4: Using Static Files Middleware in Startup.cs

To add js and css files in ASP.net Core using Static Files Middleware in Startup.cs, follow these steps:

  1. Create a folder named «wwwroot» in your project’s root directory.
  2. Add your js and css files in the «wwwroot» folder. For example, if you have a file named «styles.css», put it in «wwwroot/css/styles.css».
  3. In your HTML file, add the following code to reference the css file:
link rel="stylesheet" href="~/css/styles.css" />
script src="~/js/script.js"> script>

Note: The tilde (~) character is used to specify the root directory of your project.

Here’s the complete code for the Configure method in Startup.cs:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)  // other middleware app.UseStaticFiles(); // other middleware >

And here’s an example HTML file that references a css and js file:

DOCTYPE html> html> head> title>My Pagetitle> link rel="stylesheet" href="~/css/styles.css" /> head> body> h1>Hello World!h1> script src="~/js/script.js"> script> body> html>

That’s it! With these steps, you can easily add js and css files in ASP.net Core using Static Files Middleware in Startup.cs.

Источник

Gray Pipe

Answered by:

Question

User-74517680 posted
ok this is the code of my aspz page that is connected to the master page i know where to place the code in masterpage between the section to connect it to a css stylesheet but about this aspx that is already connected to masterpage i dont know where to put the code for connection with stylesheet.css
this is the code og my aspx page please guide me where to put the code and what code to connect it to a stylesheet —————————————————————————————————————————

Answers

User1643008609 posted
Hi Friend, Visit this link this will answer your question for sure. You can actuall add that from code behind also and in aspx also both method explained bellow http://forums.asp.net/p/1084411/3289511.aspx#3289511 http://forums.asp.net/p/965641/1206638.aspx I actually add that in first content in content Page as shown following which is also fine. < asp:content contentplaceholderid = "head" runat = "server" > < link rel = "stylesheet" href = "css/lightbox.css" type = "text/css" media = "screen"/> prasadP

User-939069355 posted
If you already have a .css file and a .master file that is already linked to your .css file. To make your .aspx file to be able to use your .master file, the code in your .aspx file has to be in this.

You would have to edit the code to link to your master file, and what is the name of your .aspx file. For instance, the code I gave you, my .aspx file name isDefault.aspx and my .master file name is site.master.
Someone correct me if Im wrong, thanks 🙂

Источник

Читайте также:  Welcome to the jungle
Оцените статью