每次我的api开始时,它都是用LOCALHOST:PORT/api/values/
执行的。如何使用静态主页LOCALHOST:PORT/
?
你在找这样的东西吗?
$.ajax({
url: "/api/values/METHOD/?PARAM=0",
type: "GET",
dataType: "json",
cache: false,
statusCode: {
200: function (data) {
//Do stuff
}
}
});
解决方案上下文中运行的任何内容都将从根开始。
在项目中,找到launchSettings.json文件。在visual studio中,您需要展开“属性”以从解决方案资源管理器中查找它或使用Ctrl + T.此文件包含一系列配置文件。每个配置文件都有一个launchUrl字段,您可以在其中提及您的路径为空。
在主页中添加内容时,您始终可以按如下方式制作中间件:
app.Use(async (context, _next) => {
if (string.IsNullOrEmpty(context.Request.Path.ToString())
|| context.Request.Path.ToString() == "/")
{
context.Response.StatusCode = 200;
await context.Response.WriteAsync("Web API is now running.");
}
else
await _next();
});
你总是可以有一个动作,但我建议使用像上面这样的中间件。
可能与How to set start page in dotnet core web api?重复
我假设你的意思是当用户导航到http://localhost而不是调用http://localhost/api/controller时有一个默认页面。
在.net核心2中,它很容易做到。如果您只想通过添加来显示简单的静态页面,则可以使用静态文件
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
...other code here...
app.UseDefaultFiles(new DefaultFilesOptions { DefaultFileNames = new List<string> { "index.html" } });
app.UseDefaultFiles();
app.UseStaticFiles();
}
并确保wwwroot文件夹中有一个index.html。
或者你可以在mvc中使用路由
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}");
});
看看aslan在https://stackoverflow.com/a/40651363/3786363的回答
哦,除非您的服务器映射到端口80,否则您可能需要调用localhost:port而不仅仅是localhost。