【问题标题】:asp.net web api methods are not getting executedasp.net web api 方法没有被执行
【发布时间】:2021-10-15 07:10:21
【问题描述】:

我正在使用 jQuery 在 asp.net mvc 中创建一个 web 应用程序,我创建了一个 mvc 应用程序并包含一个 web api 控制器,现在我的控制器如下所示

public class DefaultController : ApiController
{
    [HttpGet]
    [Route("Default/test")]
    public string test()
    {
        return "1";
    }
}

应用程序的网址

https://localhost:44308/Home/about

但是当我执行我的api的方法时,会出现以下错误

无法访问此网站

我也尝试过使用 ajax 调用,但仍然是同样的问题

$.ajax({
    type: 'GET',
    url: 'https://localhost:44308/Default/test',
    async: false,
    global: false,
    contentType: "application/json",
    success: function (returnedData) {
        console.log(returnedData)
    }
});

我不明白我在这里做错了什么

【问题讨论】:

    标签: asp.net-web-api webapi


    【解决方案1】:

    更改路由属性

        [Route("~/Default/test")]
        public string test()
        {
            return "1";
        }
    

    【讨论】:

      猜你喜欢
      • 2017-11-27
      • 2022-11-12
      • 2017-08-24
      • 1970-01-01
      • 1970-01-01
      • 2013-12-07
      • 2017-02-26
      • 2021-05-04
      • 1970-01-01
      相关资源
      最近更新 更多