微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net – Web api界面在本地工作,但不在Azure上

我的情况与 this question非常相似,但由于他没有得到答案,我以为我会投入更多的投入。

一切都在本地工作(在VS嵌入式服务器上)。当我部署到Azure时,我收到一个404错误,伴随着“没有找到与控制器名称匹配的类型”。

然而,当我加载routedebugger模块时,即使在Azure上,映射似乎也是可以的。

我可以做什么来调试这个问题?

谢谢,

亚历克斯

编辑:我的路由是这样创建的:

GlobalConfiguration.Configuration.Routes.MapHttpRoute(
                name: "DefaultApi",routeTemplate: "api/{controller}/{id}",defaults: new { id = RouteParameter.Optional }
            };
            GlobalConfiguration.Configuration.Routes.MapHttpRoute(
                name: "ActionApi",routeTemplate: "api/{controller}/{action}/{id}",defaults: new { id = RouteParameter.Optional }
            );

编辑2:这里我的控制器类

public class EmployeeController : ApiController
{
    // GET api/<controller>
    public IEnumerable<Employee> Get()
    {
        using (var context = new nws())
        {
            return context.Employees;
        }
    }

    // GET api/<controller>/5
    public Employee Get(int id)
    {
        using (var context = new nws())
        {
            return context.Employees.FirstOrDefault(e => e.ID == id);
        }
    }

    // GET api/<controller>/getbyatid/5
    public Employee GetByAtId(string id)
    {
        using (var context = new nws())
        {
            return context.Employees.FirstOrDefault(e => e.AtUserID == id);
        }
    }

    // POST api/<controller>
    public void Post([FromBody]string value)
    {
    }

    // PUT api/<controller>/5
    public void Put(int id,[FromBody]string value)
    {
    }

    // DELETE api/<controller>/5
    public void Delete(int id)
    {
    }

    // GET api/<controller>/timebank/5
    public int? GetTimeBank(string id)
    {
        using (var context = new nws())
        {
            var employee = context.Employees.FirstOrDefault(e => e.AtUserID == id);
            if (employee != null)
                return employee.GetTimeBank();
            return null;
        }
    }
}

解决方法

切换路线的顺序,然后重试。
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
            name: "ActionApi",defaults: new { id = RouteParameter.Optional }
        );
        GlobalConfiguration.Configuration.Routes.MapHttpRoute(
            name: "DefaultApi",defaults: new { id = RouteParameter.Optional }
        };

原文地址:https://www.jb51.cc/aspnet/252225.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐