WEB API 2自托管主机名问题

 三毛2502858553 发布于 2023-01-11 18:13

我试图制作一个自托管的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(); 
    } 
} 
} 

谢谢您的帮助

1 个回答
  • 您应该更改baseAddress以使其端点与您的主机名匹配,或者您可以使用WeakWildcard *来匹配所有可能的主机名.

    这应该工作: string baseAddress = "http://*:80/";

    2023-01-11 18:16 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有