简体   繁体   English

如何包含用 ASP.NET 编写的 API 的起始页?

[英]How to include an start-page for an API written in ASP.NET?

I inherited a rather undocumented Visual Studio API project with many subprojects, one of which is called API , and I assume what I am looking at is ASP.net.我继承了一个相当未公开的 Visual Studio API 项目,其中包含许多子项目,其中一个称为API ,我假设我正在查看的是 ASP.net。

项目结构

Background背景

I have been working with APIs built with different different technologies and frameworks (perl, cgi, python, flask, just to name a few), but I am new ASP.net.我一直在使用使用不同技术和框架(perl、cgi、python、flask,仅举几例)构建的 API,但我是新的 ASP.net。 After days of research I got the API running in Visual Studio (VS), and it also talks with the UI.经过几天的研究,我让 API 在 Visual Studio (VS) 中运行,并且它还与 UI 对话。

Challenge挑战

Since I have two Startup Projects in VS, two browser windows open-up when hitting Start in VS, the UI and the API.由于我在 VS 中有两个启动项目,因此在 VS、UI 和 API 中点击启动时会打开两个浏览器窗口。 The UI does what it is suppose to do, but the browser with the API shows an HTTP 404 error like this UI 做了它应该做的事情,但是带有 API 的浏览器显示了这样的 HTTP 404 错误

404错误

This means that I might have various endpoints /foo and /bar defined, but not / .这意味着我可能定义了各种端点/foo/bar ,但没有/ How do I do that?我怎么做? Ideally, it should give back a json with the name and the current version of the API-(sub)project.理想情况下,它应该返回一个带有 API-(子)项目名称和当前版本的 json。

My approach up to now我到目前为止的方法

I am missing the correct ASP.net search terms in order to solve the issue myself.为了自己解决问题,我缺少正确的 ASP.net 搜索词。 I am even struggling to implement the workaround to serve a plain HTML file at / .我什至在努力实施解决方法以在/处提供纯 HTML 文件。 Where in the project tree comes the index.html ?项目树中的index.html在哪里? I tried the root folder, as well as a folder called public , but both does not work, I keep receiving the 404 error.我尝试了根文件夹以及名为public的文件夹,但两者都不起作用,我一直收到 404 错误。

Your help is very much appreciated!非常感激你的帮助!

You have two options here, for automatically documenting your endpoints.您在这里有两个选项,用于自动记录您的端点。

  1. Use the HelpPage which may be included with the Visual Studio project template使用可能包含在 Visual Studio 项目模板中的HelpPage
  2. Install swagger by using the swashbuckle nuget package使用swashbuckle nuget 包安装 swagger

If you go for the option 1 then you will probably need to play around with with project templates and choose one that fits.如果您选择选项 1,那么您可能需要使用项目模板并选择适合的模板。 Then you should copy all of your existing code into the new project template.然后,您应该将所有现有代码复制到新项目模板中。

Swagger seems to be more popular now so you should try that, especially if you are working on a modern code-base. Swagger现在似乎更受欢迎,因此您应该尝试一下,特别是如果您正在处理现代代码库。 You can add XML comments to the controller endpoints and they will appear on your swagger page, http:/localhost/swagger/ui/index .您可以向控制器端点添加 XML 注释,它们将显示在您的 swagger 页面http:/localhost/swagger/ui/index You will need to install the nuget package and follow the tutorial.您将需要安装 nuget 包并按照教程进行操作。

Assume you created a file index.html located in the root directory, in order to get that shown as (sub)project start page, right-click on the project in the Solution Explorer , choose Properties .假设您在根目录中创建了一个index.html文件,为了获得显示为(子)项目起始页的文件,请在解决方案资源管理器中右键单击该项目,选择Properties A new view will pop-up where you define the Web > Start Action to point to a Specific Page (rather than Current Page ) where you enter index.html .将弹出一个新视图,您可以在其中定义Web > Start Action以指向您输入index.html特定页面(而不是当前页面)。 Hit CTRL+S to save, and start the project.CTRL+S保存,然后启动项目。

API 属性

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

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