how to include javascript into web app projet in visual studio? - asp.net

Anyone knows how to include javascript file as project's resource for aspx.cs files under different folders to use?thanks very much

Suppose your project's directory looks like:
/App_Code
/App_Data
/App_Themes
/Master_Pages
...
web.config
Add a folder called "scripts" so you have:
/App_Code
/App_Data
/App_Themes
/Master_Pages
/scripts
...
web.config
Put your javascript file in this scripts folder.
Put the script tag in any of your aspx pages.
<script src="/ProjectRoot/scripts/your-file.js" type="text/javascript"></script>
The key to remember is the path you specify in the src attribute. This is relative to the root of your domain. If you are developing on your machine and you test your project at:
http://localhost:2430/SomeProject/
Then your script tag needs to look like:
<script src="/SomeProject/scripts/your-file.js" type="text/javascript"></script>
If your project is deployed on a server at the root: http://www.example.com/
Then your script tag needs to look like:
<script src="/scripts/your-file.js" type="text/javascript"></script>
If your project is deployed on a server at the root: http://www.example.com/SomeApp
Then your script tag needs to look like:
<script src="/SomeApp/scripts/your-file.js" type="text/javascript"></script>
FYI, because the script tag is not a server-side tag you can't use the "~/" syntax. And because you can't use the "~/" syntax, you (usually) can't use a relative path for the src attribute but instead have to specify an absolute path from the root of your domain.

Put the javascript file in a folder in the web application project.
In pages where you want to refer the javascript file you can add this in your aspx file.
<script src="path_to_yourfile" type="text/javascript"></script>

Related

Problem with css / js path is not opening in asp.net core blazor

I am shifting my development environment from windows to mac, and when I run the code after the complete setup, my website doesn't loads and doesn't shows any style and js doesn't work.
I have this path of the file, everything was working on my windows very fine but when I run it from the mac then the file path is not loading, how do I fix this? I am trying to search whole web, couldn't find this answer and ended up here :(
<link href="/_content/FileUploading.SearchEngine.Shared/content/styles/bootstrap.min.css" rel="stylesheet">
<link href="/_content/FileUploading.SearchEngine.Shared/content/styles/style.css" rel="stylesheet">
<link href="/_content/FileUploading.SearchEngine.Shared/content/styles/dashboard.css" rel="stylesheet">
<link href="/_content/FileUploading.SearchEngine.Shared/content/scripts/script.js" rel="stylesheet">
This become the url of the path of the file = https://localhost:5002/_content/FileUploading.SearchEngine.Shared/content/styles/dashboard.css
This doesn't loads.
I have this path of the file, everything was working on my windows
very fine but when I run it from the mac then the file path is not
loading, how do I fix this?
It would be nicer if you could share your configuration details regarding how you are calling the path and where is your actual resources are located. The issue you are having might be causing due to numerous reasons.
As you may know, a BlazorWebView control has a configured host file (HostPage), typically wwwroot/index.html. The HostPage path is relative to the project. All static web assets (scripts, CSS files, images, and other files) that are referenced from a BlazorWebView are relative to its configured HostPage.
Thus, web root of the HostPage determines which subset of static assets are available. Therefore, its recommended placing the HostPage at the root of the wwwroot folder of the app, which provides the greatest flexibility for supplying static assets from the app, RCLs, and via subfolders of the app and RCLs.
How to Resolve:
Static File Middleware configuration :
Let's consider, your resouce files are outside of your wwwroot folder; Therefore, In non-IIS hosting and reverse proxy hosting scenarios, additional Static File Middleware configuration might be required to serve static files correctly. For you scenario if your static are placed outside, please check if you are using as following:
app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(builder.Environment.ContentRootPath, "StaticFilesFolderName")),
RequestPath = "/StaticFilesFolderName"
});
Then you should refer that files as following on your _Layout.cshtml file:
<link rel="stylesheet" href="~/StaticFilesFolderName/site.css" asp-append-version="true" />
Note: Please have a look this official document.
Program.cs File:
builder.WebHost.UseStaticWebAssets();
Note: If you have static file configuration you should have UseStaticWebAssets in your program.cs file just below the builder.
Check App based path:
As explained earlier, this might happening due to your resource path while moving to MAC. In this scenario its recommended to use relative based path reference on top of your base href in _Layout.cshtml after that you can place rest of your resource file. So if your resource are inside wwwroot place your base href top of your other path reference and access it by hostname/your_resource_path.
<base href="~/" />
and following should also work:
<base href="~/YourApp/" />
Note: The trailing slash is required in some scenrio if you would get any css loading issue.
Static files in non-Development environments:
Be aware of non development environment as well because Blazor apps run locally, static web assets are only enabled by default in the Development environment therefore, to enable static files for environments other than Development during local development and testing we have to call UseStaticWebAssets on the WebApplicationBuilder.
Output:

My web app cannot find my any of my files.

My web application is inside of a folder called application and inside my index.html I am providing the project path to the css file, but it gives me a 404 not found. Any ideas?
<link rel="stylesheet" type="text/css" href="application/css/style.css">
css folder and file style.css both exist and the css folder is located inside of application. This happens to all my files.

aspnet Mvc Visual Studio 2012 aspx href

I am working on an aspnet MVC 3 project under visual 2012. I hava an Home/Index.aspx file which is referencing a remote javascripts ressource and css ressource like this:
<link rel="stylesheet" href="http://mottie.github.io/tablesorter/css/theme.bootstrap.css"/>
This is working without problem.
Now in order to boot my application pages loading, a have made a copy of those files in a local folder (Home/tablesorter) but when I reference them again it's not working:
<script type='text/javascript' src="tablesorter/js/jquery.tablesorter.js"></script>
I have tried this :
<script type='text/javascript' src="<%= Url.Content("tablesorter/js/jquery.tablesorter.js") %>"></script>
but without success.
Could you please tell me what's wrong with my code.
Thank you in advance (I am newer in .net)
Drag the JS or CSS file from folder and drop on aspx page it will automatically add the correct path.

How do I define my script tag to handle virtual directories?

In my index.html file I'm using:
<script src="/Scripts/jquery-1.7.2.js" type="text/javascript"></script>
which isn't found if my URL is http://foobar.com/virtual/index.html but is found if the URL is http://foobar.com/index.html. How can I define my script tag so that it's found if a virtual directory is used. I don't want to specify the virtual directory in the script tag because this is being deployed in several places and it may or may not have a virtual directory.
Thanks for any help. This web site has saved my butt many times.
The src tag is in reference to the html that is using it. In your current scenario if you wish the script src to work in first scenario (foobar.com/virtual/index.html), just replace script tag with <script src="../Scripts/jquery-1.7.2.js" type="text/javascript"></script>

Extjs 4 MVC - Relative path problems with App.JS finding my controller - under WEB-INF with Spring MVC

Maybe the solution of my problem is contained in the question, but basically
I'm new to ExtJs 4 MVC and am having some difficulty where to place my JSPs.
I'm using Spring MVC using the Request Mapping annotation.
I'm having relative path problems with ExtJs App.JS finding my controller.
I usually put my JSPs under WEB-INF/views and my ExtJs 4 App is in WebContent/app.
I am trying to set up the example as shown on:
http://docs.sencha.com/ext-js/4-0/#/guide/application_architecture
and in the example the index.html is contained in the app package.
So I have a view called WEB-INF/views/sample-view.jsp and this includes the app.js script, which works fine.
My app has a controller like the example, and this is the problem, because it seems to try to find that internally in ExtJs using the appFolder, and controllers.
This resource can't be found, because my view is in another package all together. Does this make sense ?
Should I just move my view out of WEB-INF ? And if so will Spring MVC complain?
Thanks,
Lisa
For the first attempt at getting this running it is easiest to use all static files just to keep things simple. Once the app is running from static html and js files, migration can then be made to use the spring mvc and jsp pages.
To start be sure there is a folder named resources under the webapp folder, assuming webapp is the parent of your existing WEB-INF folder.
The basic starting folder structure for using a static html page will be:
webapp
- resources
-- app
-- css
--- ext-all.css
-- sass
-- themes
- WEB-INF
-- spring
-- classes
-- views
index.html
app.js
ext-all-debug.js
In WEB-INF there is likely a spring folder with an mvc-config.xml file or similar. In that config file the resources folder needs to be designated for serving static content by using the resources tag. Likely, the first mvc:annotation-driven tag is alrleady in the config file as in this snipped below. Add the resources tag noted below into the config file.
<!-- Configures support for #Controllers -->
<mvc:annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
With this in place the resources folder can serve the index.html file much like setup of the Sencha example.
All of this is setting you up to be able to build the first example from a url like:
http://localhost:8080/resources/index.html
When downloaded and extracted, the Ext JS zip, contains a resources folder. Copy the subfolders of that folder into the webapp/resources folder. Also copy ext-all.debug.js to the webapp/resources folder.
Then create index.html in webapp/resources with this content similar to the Sencha tutorial:
<html>
<head>
<title>Static Account Manager</title>
<link rel="stylesheet" type="text/css" href="./css/ext-all.css">
<script type="text/javascript" src="./ext-all-debug.js"></script>
<script type="text/javascript" src="./app.js"></script>
</head>
<body></body>
</html>
Create app.js in the webapp/resources folder as the following snippet and enough is in place to be up and running with the single panel configured in app.js. From this point the tutorial is easy enough to port over to this setup.
Ext.application({
name: 'AM',
appFolder: 'app',
launch: function() {
Ext.create('Ext.container.Viewport', {
layout: 'fit',
items: [
{
xtype: 'panel',
title: 'Users',
html : 'List of users will go here'
}
]
});
}
});
After that static html file is running correctly, a jsp can be used from the view folder and will have content like this:
<html>
<head>
<title>JSP Account Manager</title>
<link rel="stylesheet" type="text/css" href="./resoures/css/ext-all.css">
<script type="text/javascript" src="./resoures/ext-all-debug.js"></script>
<script type="text/javascript" src="./resoures/app.js"></script>
</head>
<body></body>
</html>
a bit off topic, but if you are going to use Spring MVC might as well use Grails - you will save yourself a lot of typing and figuring out bunches of config files.

Resources