简体   繁体   English

如何在libman(库管理器)asp.net Core 2.1中使用Microsoft.jQuery.Unobtrusive.Ajax?

[英]How do I use Microsoft.jQuery.Unobtrusive.Ajax with libman (Library manager) asp.net Core 2.1?

I am trying to use unobtrusive ajax to update my partial views. 我试图使用不显眼的ajax来更新我的部分视图。 Unfortunately I ran in to a problem when I was about to install the package, since Bower is not working (?) or recommended anymore according to https://docs.microsoft.com/en-us/aspnet/core/client-side/bower?view=aspnetcore-2.1 . 不幸的是,当我要安装软件包时遇到问题,因为Bower根据https://docs.microsoft.com/en-us/aspnet/core/client-side不再工作(?)或推荐/bower?view=aspnetcore-2.1 Instead they recommend us to use Libman. 相反,他们建议我们使用Libman。

I followed the steps from https://www.c-sharpcorner.com/article/unobtrusive-ajax-and-jquery-for-partial-updates-in-asp-net-mvc/ . 我按照https://www.c-sharpcorner.com/article/unobtrusive-ajax-and-jquery-for-partial-updates-in-asp-net-mvc/中的步骤进行操作。

So I have installed the nuget Microsoft.jQuery.Unobtrusive.Ajax-package, but how do I reference the ajax-package with libman? 所以我已经安装了nuget Microsoft.jQuery.Unobtrusive.Ajax-package,但是如何使用libman引用ajax-package?

I looked at this How to reference Microsoft.JQuery.Unobtrusive.Ajax within my ASP.NET Core MVC project answer but it only shows how to use Bower. 我看了这个如何在我的ASP.NET核心MVC项目答案中引用Microsoft.JQuery.Unobtrusive.Ajax,但它只显示了如何使用Bower。

I struggled with LibMan at first too. 我起初也和LibMan斗争过。 I found this guide that points out there's a GUI portion of LibMan . 我发现本指南指出了LibMan的GUI部分 Using the UI portion of LibMan under the "project right click menu -> Add -> Client-Side Library" helped me figure out better ways to define which files I want and change the provider easier. 在“项目右键菜单 - >添加 - >客户端库”下使用LibMan的UI部分帮助我找到更好的方法来定义我想要的文件并更容易地更改提供程序。

I ended up having most of my files come from cdnjs, but I set up jquery-ajax-unobtrusive to come from unpkg like so: 我最终得到的大部分文件来自cdnjs,但我设置了jquery-ajax-unobtrusive来自unpkg,如下所示:

{
    "provider": "unpkg",
    "library": "jquery-ajax-unobtrusive@3.2.6",
    "destination": "wwwroot/lib/jquery-ajax-unobtrusive/"
}

The answer by @mybirthname is great . @mybirthname的答案很棒。 Another way to do that is to use libman cli . 另一种方法是使用libman cli。 We can use the following command to install the libman : 我们可以使用以下命令来安装libman:

dotnet tool install --global Microsoft.Web.LibraryManager.Cli

And now you can install jquery , jquery-validation-unobtrusive and so on as you like : 现在您可以根据需要安装jquery,jquery-validation-unobtrusive等等:

to init a libman.json : 初始化libman.json

libman init 

to install a dependency of jquery-validation-unobtrusive : 安装jquery-validation-unobtrusive的依赖项:

> libman install jquery-validation-unobtrusive
Destination [lib\jquery-validation-unobtrusive]:
lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js written to disk
lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js written to disk
Installed library "jquery-validation-unobtrusive@3.2.10" to "lib\jquery-validation-unobtrusive" 

to install a depenency of jquery : 安装jquery的依赖:

> libman install jquery
Destination [lib\jquery]:
lib/jquery/core.js written to disk
lib/jquery/jquery.js written to disk
lib/jquery/jquery.min.js written to disk
lib/jquery/jquery.min.map written to disk
lib/jquery/jquery.slim.js written to disk
lib/jquery/jquery.slim.min.js written to disk
lib/jquery/jquery.slim.min.map written to disk
Installed library "jquery@3.3.1" to "lib\jquery"

[Edit] [编辑]

To install jquery-ajax-unobtrusive on npm, since it's not yet on cdnjs, we can use unpkg provider : 要在npm上安装jquery-ajax-unobtrusive ,因为它还没有在cdnjs上,我们可以使用unpkg提供程序:

unpkg is a fast, global content delivery network for everything on npm unpkg是一个快速的全球内容交付网络,适用于npm上的所有内容

libman install -p unpkg jquery-ajax-unobtrusive

You could use npm. 你可以使用npm。 Add pakage.json file in the root of your web project 在您的Web项目的根目录中添加pakage.json文件

{
  "version": "1.0.0",
  "name": "your-system",
  "devDependencies": {
    "jquery-ajax-unobtrusive": "^3.2.4"
  },
  "exclude": [
  ]
}

Now everything related to the library will be automatically downloaded in node_modules/jquery-validation-unobtrusive . 现在,与库相关的所有内容都将自动下载到node_modules/jquery-validation-unobtrusive

Be aware the node_module folder is not part of the project so you need to click Show All Files to see all folders. 请注意,node_module文件夹不是项目的一部分,因此您需要单击“ Show All Files以查看所有文件夹。

在此输入图像描述

After that if you want to always have latest version of the library instead of copying the file to your js folder you could use bundle config. 之后,如果您想要始终拥有最新版本的库而不是将文件复制到js文件夹,则可以使用bundle config。 Run this: 运行这个:

Install-Package BuildBundlerMinifier -Version 2.8.391

After that create json file - bundleconfig.json in the root of your web project 之后,在您的Web项目的根目录中创建json文件 - bundleconfig.json

[ 
  {
    "outputFileName": "wwwroot/js/myjs.min.js",
    "inputFiles": [
      "node_modules/jquery-ajax-unobtrusive/jquery.unobtrusive-ajax.min.js"
    ]
  }

]

This will create on every build myjs.min.js file in your js folder in wwwroot 这将在wwwroot js文件夹中的每个构建myjs.min.js文件上创建

Microsoft has written a help document on Managing Client-Side Library. Microsoft已编写有关管理客户端库的帮助文档。

https://docs.microsoft.com/en-us/aspnet/core/client-side/libman/libman-vs?view=aspnetcore-2.2 https://docs.microsoft.com/en-us/aspnet/core/client-side/libman/libman-vs?view=aspnetcore-2.2

It has steps to use the 'Add Client-Side Library dialog' in Visual Studio. 它具有在Visual Studio中使用“添加客户端库对话框”的步骤。 In Solution Explorer, right-click the project folder in which the files should be added. 在“解决方案资源管理器”中,右键单击要添加文件的项目文件夹。 Choose Add > Client-Side Library. 选择“添加”>“客户端库”。 The Add Client-Side Library dialog appears. 将出现“添加客户端库”对话框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在我的 ASP.NET Core MVC 项目中引用 Microsoft.JQuery.Unobtrusive.Ajax - How to reference Microsoft.JQuery.Unobtrusive.Ajax within my ASP.NET Core MVC project ASP.NET Core 2.1 不显眼的 Ajax 验证不适用于部分视图表单交换 - ASP.NET Core 2.1 Unobtrusive Ajax Validation Not Working With Partial View Form Swap 在 ASP.NET Core 中将 ValidateAntiForgeryToken 属性与 Unobtrusive Ajax 插件一起使用 - Using ValidateAntiForgeryToken attribute with Unobtrusive Ajax plugin in ASP.NET Core 如何在 ASP.NET Core 2.1 中使用角色? - How to use Roles in ASP.NET Core 2.1? ASP.NET Core 2.1角色管理器注册 - ASP.NET Core 2.1 Role Manager Register 如何解释 ASP.NET Core 2.1 中的 Serilog 配置? - How do I interpret Serilog configuration in ASP.NET Core 2.1? 如何在 asp.net core 2.1 中使用自定义消息设置状态代码? - How do I set the status code with custom message in asp.net core 2.1? 如何在 Identity ASP.NET Core 2.1(Identity Scaffolded)中播种用户角色 - How do I seed user roles in Identity ASP.NET Core 2.1 (Identity Scaffolded) ASP.NET Core 2.1将json列表传递给ajax - ASP.NET Core 2.1 passing json list to ajax ASP.NET Core 2.1从类库引用ApplicationDbContext - ASP.NET Core 2.1 Referring to ApplicationDbContext from Class Library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM