我试图制作一个自托管的web api服务.我按照教程,它在我的本地计算机上工作正常.
localhost/api/values对预期的JSON响应很好.
现在,我有一个绑定到DNS"myserver.mycompany.com"的服务器.当我在此服务器上启动WebApi 2服务并尝试调用myserver.mycompany.com/api/values时,我找不到404页面错误.
如果我在这个服务器上本地浏览并调用localhost/api/values url它可以正常工作.
这是Startup类的代码:
using Owin; using System.Web.Http; namespace SelfHostedWebApi2 { public class Startup { // This code configures Web API. The Startup class is specified as a type // parameter in the WebApp.Start method. public void Configuration(IAppBuilder appBuilder) { // Configure Web API for self-host. HttpConfiguration config = new HttpConfiguration(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); appBuilder.UseWebApi(config); } } }
以下是我启动服务器的方式:
using Microsoft.Owin.Hosting; using System; using System.Net.Http; namespace SelfHostedWebApi2 { public class Program { static void Main() { string baseAddress = "http://localhost:80/"; // Start OWIN host try { WebApp.Start(new StartOptions(url: baseAddress)); HttpClient client = new HttpClient(); var response = client.GetAsync(baseAddress + "api/values").Result; Console.WriteLine(response); Console.WriteLine(response.Content.ReadAsStringAsync().Result); } catch (Exception ee) { Console.WriteLine("Erreur : " + ee.ToString()); } Console.ReadLine(); } } }
谢谢您的帮助
您应该更改baseAddress以使其端点与您的主机名匹配,或者您可以使用WeakWildcard *
来匹配所有可能的主机名.
这应该工作: string baseAddress = "http://*:80/";