简体   繁体   中英

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

We have a server application that uses Web API 2 to expose restful API. To help clients use our API better, we developed a js library that wraps API calls. Instead of distributing the js library to the clients, we'd like them to reference them directly from the server.

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)

This isn't something an Api should concern itself with. 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.
  2. Distribute it as an npm package or nuget package and let them reference it that way.

You could expose it from the api itself but there are a number of issues with that approach. 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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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