在ASP.NET的WebAPI重定向路径 [英] Redirecting route in ASP.NET WebAPI

查看:3105
本文介绍了在ASP.NET的WebAPI重定向路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是确定的:

    GlobalConfiguration.Configuration.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{action}",
        defaults: new { id = System.Web.Http.RouteParameter.Optional });

不过,我想重定向进入的请求ApiFolder文件夹中的类

But I want to redirect incoming requests to ApiFolder folder's class

推荐答案

作为评论指出肯尼斯的建议无法工作。

Kenneth's suggestion wont work as stated in the comments.

然而,你可以写自己的实现IHttpControllerSelector的,只分配它,当你映射API路线。我用的这篇文章为基础,修改了它。

However you can write your own implementation of IHttpControllerSelector and only assign it when you map the api routes. I used the implementation in this article as a base and modified it.

那么它就是这样的映射WebApiConfig路由后替换默认选择器的小问题(其中CustomControllerSelector是我实现):

Then its just the small issue of replacing the default selector after mapping the route in WebApiConfig like this (where CustomControllerSelector is my implementation):

    public static void Register(HttpConfiguration configuration)
    {
        var apiRoute = configuration.Routes.MapHttpRoute(
            name: "API Default",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional, controllerNamespace = "api" }
        );

        configuration.Services.Replace(typeof(IHttpControllerSelector), new CustomControllerSelector(configuration));
    }

这篇关于在ASP.NET的WebAPI重定向路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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