首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Web API路由-仅具有URI参数的操作有效

Web API路由-仅具有URI参数的操作有效
EN

Stack Overflow用户
提问于 2014-07-06 09:19:20
回答 1查看 1.4K关注 0票数 1

我有一个web API项目,它运行得很好。我将它与一个MVC项目合并,现在只有带有URI参数的操作才能工作。所有其他操作都以404 Not found结束,其中甚至找不到控制器。

下面是我在WebApiConfig中拥有的(标准的东西):

代码语言:javascript
复制
public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
        // Configure Web API to use only bearer token authentication.
        config.SuppressDefaultHostAuthentication();
        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

        // Web API routes
        config.MapHttpAttributeRoutes();

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

下面是控制器类:

代码语言:javascript
复制
[Authorize]
[RoutePrefix("api/WikiPlan")]
public class WikiPlanController : ApiController

以下是有效的操作:

代码语言:javascript
复制
http://localhost:2000/api/WikiPlan/SearchWikiPlans/baby
[AllowAnonymous]
[HttpGet]
[Route("SearchWikiPlans/{keyword}")]
[ResponseType(typeof(List<WikiPlanSearchResultViewModel>))]
public IHttpActionResult SearchWikiPlans(string keyword)

这里有一个不起作用的(当它在自己的项目中时,它曾经起作用):

代码语言:javascript
复制
http://localhost:2000/api/WikiPlan/TopWikiPlans
[AllowAnonymous]
[HttpGet]
[Route("TopWikiPlans")]
[ResponseType(typeof(List<TopWikiPlan>))]
public IHttpActionResult TopWikiPlans()

我不知道哪里出了问题。谢谢你的帮忙!

EN

回答 1

Stack Overflow用户

发布于 2014-07-06 10:57:51

多亏了这个路由调试器工具(http://blogs.msdn.com/b/webdev/archive/2013/04/04/debugging-asp-net-web-api-with-route-debugger.aspx),我能够跟踪损坏的网址并解决问题。

事实证明,框架是根据MVC路由而不是我的API路由匹配损坏的URL。因此,我移动了调用以在Global.asax中的MVC路由之上注册API路由,现在可以正确匹配了。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24591888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档