asp.net webapi 2 属性路由不起作用 [英] asp.net webapi 2 attribute routing not working

查看:41
本文介绍了asp.net webapi 2 属性路由不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 .net framework 4.5 安装了带有 mvc4 的 Visual Studio 2012.现在我想将 webapi2 与属性写入一起使用,并且我希望我的 hlep 页面正确显示所有端点.

I have visual studio 2012 installed with mvc4 using .net framework 4.5. Now I want to use webapi2 with attribute writing and i want my hlep page show all the endpoints properly.

在我的解决方案中,我添加了一个新的 mvc4 基础 emtpy 项目,并使用 nuget 升级到 mvc5,然后我安装了 webapi2 包.最后,我为 webapi2 安装了帮助包.

In my solution i added a new mvc4 base emtpy project and using nuget i upgraded to mvc5 and then i have installed webapi2 packages. lastly i have installed help package for webapi2.

现在,当我使用 routeprefix 时,我在帮助页面上看不到任何内容,当我尝试在浏览器中访问我的 webapi 端点时,它会引发以下错误.

now when i use routeprefix I cant see any content on help page and when i try to access my webapi endpoint in browsers it throws following error.

http://expressiis.com/api/v1/

   <Error>
    <Message>
    No HTTP resource was found that matches the request URI 'http://expressiis.com/api/v1/'.
    </Message>
    <MessageDetail>
    No type was found that matches the controller named 'v1'.
    </MessageDetail>
    </Error>

namespace WebApi.Controllers
{
    [RoutePrefix("api/v1")]
    public class SubscribersController : ApiController
    {
        // GET api/<controller>   
        [Route("")]
        [HttpGet]
        public IQueryable<string> Get()
        {
            return new string[] { "value1", "value2" }.AsQueryable();
        }


    }
}

推荐答案

根据您的信息,您似乎没有调用 httpConfig.MapHttpAttributeRoutes() (确保调用此 任何传统的路由注册之前)

Based on your information, it looks like you are not calling the httpConfig.MapHttpAttributeRoutes() (Make sure to call this before any traditional routing registrations)

由于您还没有调用 MapHttpAttributeRoutes,您的请求似乎与传统路由匹配,例如 api/{controller}.这是行不通的,因为匹配传统路由的路由永远不会看到用属性路由修饰的控制器/动作.

Since you haven't called MapHttpAttributeRoutes, your request seems to be matching a traditional route, for example, like api/{controller}. This will not work because routes matching traditional routes will never see controllers/actions decorated with attribute routes.

这篇关于asp.net webapi 2 属性路由不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆