웹 API 2 자체 호스트 호스트 이름 문제
-
21-12-2019 - |
문제
i'am 자체 호스팅 된 웹 API 서비스를 만들려고합니다.나는 튜토리얼을 따랐고, 로컬 컴퓨터에서 잘 작동합니다.
localhost / api / 값은 예상 된 JSON으로 잘 응답합니다.
이제 서버가 DNS "myserver.mycompany.com"에 바인딩되었습니다.이 서버에서 WebApi 2 서비스를 시작하고 myserver.mycompany.com/api/values를 호출하려고하면 404 페이지가 없습니다.
이 서버에서 로컬로 찾아보고 LocalHost / API / VALUES URL을 호출하면 잘 작동합니다.
여기에 시작 클래스의 코드가 있습니다.
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<Startup>(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();
}
}
}
.
도움말 q / p>에 감사드립니다
해결책
BaseAddress를 변경하여 엔드 포인트가 호스트 이름과 일치하도록 또는 모든 가능한 호스트 이름과 일치하도록 DeakWildCard *
를 사용할 수 있습니다.
이 작업이 작동해야합니다 : string baseAddress = "http://*:80/";
제휴하지 않습니다 StackOverflow