0

我用谷歌搜索了很多具有相同问题的页面,但我无法解决我的问题。所以我在控制器中有这个

public IEnumerable<questions> Get()
    {
        return (UOW.Questions.GetAll());
    }

    // GET api/questions/5
    public questions Get(int id)
    {
        return (UOW.Questions.GetById(id));
    }

    ////Basic Questions :: api/questions/basic
    [ActionName("basic")]
    public IEnumerable<questions> GetBasicQuestions()
    {
        return (UOW.Questions.GetBasicQuestions());
    }

    [ActionName("basic")]
    public questions GetBasicQuestionsById(int id)
    {
        return (UOW.Questions.GetBasicQuestions(id));
    }

这在 WEBApiConfig 中:

public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultController",
            routeTemplate: "api/{controller}"
        );

        config.Routes.MapHttpRoute(
            name: "DefaultControllerId",
            routeTemplate: "api/{controller}/{id}",
            defaults: null,
            constraints: new { id = @"^\d+$" }
        );

        config.Routes.MapHttpRoute(
            name: "DefaultAction",
            routeTemplate: "api/{controller}/{action}"
        );

        config.Routes.MapHttpRoute(
            name: "DefaultActionId",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { action = RouteParameter.Optional }
        );
    }

当我输入 localhost/api/questions 和 localhost/api/questions/1 时,我不明白

给我多个路线错误...

谢谢..

4

1 回答 1

0

将您的路由更改如下:

config.Routes.MapHttpRoute(
    name: "DefaultController",
    routeTemplate: "api/{controller}",
    defaults: new { action = "GET" }
);

config.Routes.MapHttpRoute(
    name: "DefaultControllerId",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { action = "GET" },
    constraints: new { id = @"^\d+$" }
);

config.Routes.MapHttpRoute(
    name: "DefaultAction",
    routeTemplate: "api/{controller}/{action}"
);

config.Routes.MapHttpRoute(
    name: "DefaultActionId",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
);
于 2013-03-27T09:54:17.343 回答