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

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

问题描述

我正在创建一个包含多个区域的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核心内容文件所在的位置。告诉asp.net核心的方法是配置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.

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

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