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

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

问题描述

我使用.net Framework 4.5在Visual Studio 2012上安装了mvc4.现在,我想将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天全站免登陆