简体   繁体   English

如何从ac#服务器应用程序提供JavaScript文件?

[英]How to serve JavaScript files from a c# server application?

We have a server application that uses Web API 2 to expose restful API. 我们有一个使用Web API 2公开静态API的服务器应用程序。 To help clients use our API better, we developed a js library that wraps API calls. 为了帮助客户更好地使用我们的API,我们开发了一个包装API调用的js库。 Instead of distributing the js library to the clients, we'd like them to reference them directly from the server. 与其将js库分发给客户端,我们不希望它们直接从服务器引用它们。

To make it easy on clients, I'd like this line to work: 为了方便客户,我希望此行有效:

<script src="serverUri/scriptsFolder/clientLibrary.js">

Is that possible? 那可能吗? (using Web API 2, we'd prefer to have as fewer dependencies as possible) (使用Web API 2,我们希望有尽可能少的依赖关系)

This isn't something an Api should concern itself with. 这不是Api应该关注的事情。 Basically I would choose another delivery mechanism. 基本上,我会选择其他交付机制。

The way I see it you have two options : 从我的角度来看,您有两种选择:

  1. put it on a CDN somewhere and provide your client with the link to it - which you would have to do anyway. 将其放在CDN上,并为您的客户提供指向它的链接-无论如何您都必须这样做。
  2. Distribute it as an npm package or nuget package and let them reference it that way. 将其作为npm软件包或nuget软件包分发,并让他们以这种方式引用它。

You could expose it from the api itself but there are a number of issues with that approach. 您可以从api本身公开它,但是这种方法存在很多问题。 You basically give your clients a choice with the other approaches. 您基本上可以使用其他方法为客户提供选择。 If you change the version of the file you could potentially break all your clients very quickly. 如果更改文件的版本,则可能会很快破坏所有客户端。 Controlling your delivery mechanism gives them an option to upgrade when it's convenient to them. 通过控制您的投放机制,他们可以在方便时进行升级。

To be fair I did add a scripts folder to a web api2 project, put a js file in there and I was able to access it directly with no issue, so it can be done, but I still don't think it's a good idea :) 公平地说,我确实向Web api2项目中添加了一个scripts文件夹,在其中放置了一个js文件,我可以毫无问题地直接访问它,因此可以做到,但是我仍然认为这不是一个好主意:)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM