ASP MVC 5属性的路由不登记的路线 [英] ASP MVC 5 Attribute routing not registering routes

查看:265
本文介绍了ASP MVC 5属性的路由不登记的路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

概述

目前,我正在试图获得属性的路由与我的API控制器工作。它似乎没有奏效,我不确定为什么。我不知道如果我错过了关键的一步,还是什么问题可以。

问题

我想打的本地主机/ API /用户/自定义?测试= 1 但我得到一个404(我希望这个工作)

如果我打的本地主机/ API / customapi?测试= 1 我顺利地进入我的方法

为什么第一个URL不行?

设置

我的设置如下:

CustomController.cs

  [System.Web.Mvc.Route preFIX(API)]
公共类CustomApiController:ApiController
{
    [System.Web.Mvc.Route(用户/自定义)]
    [System.Web.Http.HttpGet]
    公共异步任务< CustomResponse>获取([FromUri] CustomRequest要求)
    {
        //工作
    }
}

WebApiConfig.cs

 公共静态类WebApiConfig
{
    公共静态无效的注册(HttpConfiguration配置)
    {
        ...(JSON序列设置)...        //网页API路线
        config.MapHttpAttributeRoutes();        config.Routes.MapHttpRoute(
            名称:DefaultApi
            routeTemplate:API / {}控制器/(编号),
            默认:新{ID = RouteParameter.Optional}
            );
    }
}

RouteConfig.cs

 公共静态类RouteConfig
{
    公共静态无效的注册(RouteCollection路线,布尔registerAreas = TRUE)
    {
        如果(registerAreas)
        {
            AreaRegistration.RegisterAllAreas();
        }        //忽略路线
        ...        //注册具体路线
        routes.MapRoute(HomeUrl,家,新{控制器=家,行动=索引});
        。
        。        routes.MapRoute(
            默认,//路线名称
            {控制器} / {行动} / {ID},// URL带参数
            新{控制器=家,行动=索引,ID = UrlParameter.Optional}
            );
    }
}

的Global.asax.cs

 公共类全球:一个HttpApplication
{
    保护无效的Application_Start()
    {
        ....(应用程序启动时的东西)...        GlobalConfiguration.Configure(WebApiConfig.Register);
        BundleConfig.Register(BundleTable.Bundles);        ....(更多应用程序启动时的东西)...        RouteConfig.Register(RouteTable.Routes);
    }
}


解决方案

我是使用了错误的命名空间在我的路线。

  [System.Web.Http.Route()] //使用这个命名空间的Web API 2
[System.Web.Mvc.Route()][System.Web.Http.Route preFIX(API)] //使用这个命名空间的Web API 2
[System.Web.Mvc.Route preFIX(API)]

Overview

I'm currently trying to get attribute routing to work with my api controllers. It doesn't appear to be working, and I'm unsure why. I'm not sure if I'm missing a crucial step, or what the problem could be.

Problem

I'm trying to hit localhost/api/user/custom?test=1 but I get a 404 (I expect this to work)

If I hit localhost/api/customapi?test=1 I successfully get into my method

Why does the first url not work?

Setup

My setup is as follows:

CustomController.cs

[System.Web.Mvc.RoutePrefix("api")]
public class CustomApiController : ApiController
{
    [System.Web.Mvc.Route("user/custom")]
    [System.Web.Http.HttpGet]
    public async Task<CustomResponse> Get([FromUri] CustomRequest request)
    {
        //Work
    }
}

WebApiConfig.cs

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        ...(json serializer settings)...

        // Web API routes
        config.MapHttpAttributeRoutes();

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

RouteConfig.cs

public static class RouteConfig
{
    public static void Register(RouteCollection routes, bool registerAreas = true)
    {
        if(registerAreas)
        {
            AreaRegistration.RegisterAllAreas();
        }

        //Ignore Routes
        ...

        //Register specific routes
        routes.MapRoute("HomeUrl", "home", new { controller = "Home", action = "Index" });
        .
        .

        routes.MapRoute(
            "Default", //Route name
            "{controller}/{action}/{id}",  //URL with parameters
            new { controller = "Home", action = "Index", id =UrlParameter.Optional }
            );
    }
}    

Global.asax.cs

public class Global : HttpApplication
{
    protected void Application_Start()
    {
        ....(app startup stuff)...

        GlobalConfiguration.Configure(WebApiConfig.Register);
        BundleConfig.Register(BundleTable.Bundles);

        ....(more app startup stuff)...

        RouteConfig.Register(RouteTable.Routes);
    }
}

解决方案

I was using the wrong namespace in my routes.

[System.Web.Http.Route("")]   //Use this namespace for Web API 2
[System.Web.Mvc.Route("")]

[System.Web.Http.RoutePrefix("api")]  //Use this namespace Web API 2
[System.Web.Mvc.RoutePrefix("api")]

这篇关于ASP MVC 5属性的路由不登记的路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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