Reputation: 759
I would like to know how to access or create a javascript file outside www root folder in .net core project created in visual studio 2017. i have created a file in the folder where my views are but i can't able to access the file.
Upvotes: 2
Views: 2688
Reputation: 12470
You can actually do this in .net core natively.
I'm assuming you have the static files middleware up and running already, but just incase, run the following in your nuget package manager :
Install-Package Microsoft.AspNetCore.StaticFiles
Inside your startup.cs, you will have a Configure method. It probably looks not too dissimilar to the following :
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseStaticFiles();
app.UseMvcWithDefaultRoute();
}
So the UseStaticFiles will serve things from the wwwroot. But you can actually add multiple uses of the same middleware in the pipeline. So consider the following :
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"content", @"js")),
RequestPath = new PathString("/content/js")
});
app.UseMvcWithDefaultRoute();
}
Here we serve static content from the wwwroot as per normal, but we also want to say that the file location of /Content/Js should be served as well. You can add as many of these as you like and serve static content.
More info
Upvotes: 1
Reputation: 2400
ASP.NET exposes all your static filse from the wwwroot folder. It's advisable to put your files there 99% of the time. However, If your project really requires that you put your javascript file in the views folder (and I don't see why), you can create a middleware. your middleware just has to be an extension method for the IApplicationBuilder interface
for this, you can create a static class like so
// your middleware (static class so it can have extension methods)
public static class MyStaticFileServer
{
//static method (IApplicationEnvironment so we can easily get the full path of our Project)
public static IApplicationBuilder UseJsFiles(this IApplicationBuilder app, IApplicationEnvironment env)
{
//
var path = Path.Combine(env.ApplicationBasePath, "/Views/Js")
var provider = new PhysicalFileProvider(path);
var options = new StaticFileOptions();
//so our middleware only responds when the url begins with "/Views/js"
options.RequestPath = "/Views/Js";
// file provider so we can serve files
options.FileProvider = provider
//use static file middleware to our middleware to serve files from whatever folder we specify
app.UseStaticFiles(options)
return app;
}
}
Now you're done with the middleware to serve static files from a folder of your choice. Now, we ned to invoke our middleware from the startup class. We do this by invoking it in the configure class in the Startup class
//ASP.NET Configure Method in the Startup.cs file
public void Configure(IApplicationEnvironment environment)
{
app.UseJsFiles(environment);
}
Upvotes: 1
Reputation: 943571
The point of the root directory is to expose files over HTTP.
If the file isn't exposed over HTTP, then it doesn't have a URL.
If it doesn't have a URL, the browser can't request it.
Move the file into the root directory.
Upvotes: 1