Web api接口在本地工作,但不在Azure上工作

时间:2013-01-24 05:37:00

标签: asp.net azure asp.net-web-api-routing

我的情况与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;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

切换路线的顺序,然后重试。

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