JavaScript 文件在 ASP.NET Core 区域中的位置 [英] Location of JavaScript files in ASP.NET Core Areas

查看:43
本文介绍了JavaScript 文件在 ASP.NET Core 区域中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个包含多个区域的 ASP.NET Core 应用程序.我将在哪里添加特定于某个区域的 JavaScript 文件(通常我将它们放入 wwwroot\js 文件夹.某个区域是否有类似的内容?)?

I am creating an ASP.NET Core application that will contain several areas. Where would I add JavaScript files that are specific to a certain area (usually I put them into the wwwroot\js Folder. Is there something like this for an area?)?

推荐答案

我应该在哪里添加 JavaScript 文件"?答案是您可以根据您的要求和方便程度来选择您的位置.默认位置是内容根目录,即 wwwroot 文件夹及其子文件夹,但是 ASP.Net Core 不会阻止您将这些静态文件放置在wwwroot"文件夹之外.但是,如果您想将其放置在默认内容文件夹之外,即 wwwroot,您需要告诉 asp.net core 您的内容文件所在的位置.告诉asp.net core的方式是配置StaticFiles中间件如下:

"Where would I add JavaScript files"? Answer is you can choose your location based on your requirements and convenience. Default location is content root i.e. wwwroot folder and its subfolders however ASP.Net Core doesn't stop you from placing those static files outside "wwwroot" folder. However if you want to place it outside default content folder i.e. wwwroot you need to tell asp.net core where your content files are located. Way to tell asp.net core is configure StaticFiles middleware as follows:

app.UseStaticFiles(new StaticFileOptions()
{
    FileProvider = new PhysicalFileProvider(
        Path.Combine(Directory.GetCurrentDirectory(), @"MyStaticFiles")),
    RequestPath = new PathString("/StaticFiles")
});

此处 MyStaticFiles 文件夹位于 wwwroot 之外,位于您的项目目录中.

Here MyStaticFiles folder is outside wwwroot and located just inside your project directory.

现在如果你想访问 MyStaticFiles 文件夹中的任何文件,那么它将使用以下路径.

Now if you want to access any file inside MyStaticFiles folder then it will be using following path.

http://<myapp>/StaticFiles/myscript.js

注意上面路径和中间件配置中的StaticFiles".

Note "StaticFiles" in above path and in middleware configuration.

这篇关于JavaScript 文件在 ASP.NET Core 区域中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆