【问题标题】:Force HTTP over HTTPS in WEB API DotNet在 WEB API DotNet 中强制 HTTP over HTTPS
【发布时间】:2016-11-21 19:47:12
【问题描述】:

我有一个 web api 控制器,我需要在 Java 客户端中使用它,但是证书存在问题,所以我需要使用 HTTP 而不是 HTTPS。

public static class WebApiConfig
{

    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            constraints : null,
            defaults: new { id = RouteParameter.Optional }
        );
        config.Formatters.Remove(config.Formatters.XmlFormatter);
        config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"));
    }
}


public class CommentAPIController : ApiController
{
    IItemService ise = new ItemService();
    ICategoryService ice = new CategoryService();
    // GET: api/CommentAPI
    [HttpGet]
    public IEnumerable<Category> Get()
    {
        List<Category> list = new List<Category>();
        foreach (Category item in ice.getAllCategory())
        {

            list.Add(item);



        }
        return list;
    }

感谢您的帮助

【问题讨论】:

  • 在没有'https'的情况下调用url时是否出现错误?
  • 没有页面只是一直在加载
  • 你如何托管这个应用程序?您是从 Visual Studio 运行它,还是已经在 IIS 上发布?
  • 我从 Visual Studio 运行它
  • 请编辑web api项目的属性(右键项目然后属性,或者展开项目内容双击Properties组)。然后导航到Web 屏幕,查看您的网址是否指向https。如果是,请将其更改为http:// 并重新启动。通常你必须通过使用[RequireHttps] 属性来强制https,所以你的问题似乎相反。

标签: .net rest asp.net-web-api


【解决方案1】:

请编辑 web api 项目的属性(右键单击项目然后属性,或展开项目内容并双击属性组)。

然后导航到Web 屏幕并查看您的任何网址是否指向https

如果是,请将其更改为 http:// 并重新启动。通常你必须使用[RequireHttps] 属性强制https,所以你的问题似乎相反。

根据我们在 cmets 中的讨论。最后,您一直在为 HTTPS 服务提供错误的端口,这在我指向您的确切屏幕中。

让我们把它留在这里给其他人。

【讨论】:

    猜你喜欢
    • 2019-05-01
    • 1970-01-01
    • 2010-10-31
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 2020-04-06
    • 2020-08-09
    • 1970-01-01
    相关资源
    最近更新 更多