Web Api路由处理程序更改路由数据

时间:2016-02-17 14:59:56

标签: c# asp.net-web-api url-routing asp.net-mvc-routing asp.net-web-api-routing

在web api路由中,我创建了以下路由和路由处理程序。

我试图拦截请求并在它到达实际方法之前更改一些路由数据。

然而,下面的内容并没有像我预期的那样,当运行并向api发出请求时,它会返回404 No route providing a controller name was found to match request

这实际上是否可以更改路线数据?我觉得我接近解决方案,但遗漏了一些东西。

以下是我的代码:

public class CustomHander : DelegatingHandler {
    protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) {

        var routeData = request.GetRouteData();
        var subroutes = (IEnumerable<IHttpRouteData>)routeData.Values["MS_SubRoutes"];
        var route = subroutes.First();

        var msSubRoutes = new HttpRouteValueDictionary();

        var httpRouteData = new HttpRouteData(route.Route);

        foreach (var r in route.Values) {
            httpRouteData.Values.Add(r.Key, r.Value.ToString().Replace("|", "/"));
        }

        msSubRoutes.Add("MS_SubRoutes", httpRouteData);
        var newRoute = new HttpRouteData(routeData.Route, msSubRoutes);

        request.SetRouteData(newRoute);
        var test = request.GetRouteData();

        var resp = await base.SendAsync(request, cancellationToken);
        return resp;
    }
}

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

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

        config.MessageHandlers.Add(new CustomHander());
    }
}

如果我删除下面的代码行,那么它正常工作,但显然没有更改路径数据。

request.SetRouteData(newRoute);

控制器上的操作结果示例:

[Route("api/Makes")]
[HttpGet]
public IHttpActionResult Makes()
{
    using (var db = new eCatEntities())
    {
        var makes = db.VEHICLEs.Select(p => new { TEXT = p.VEHI_MAKE }).Distinct().OrderBy(p => p.TEXT);

        return Json(makes.ToList());
    }
}

[Route("api/Models/{make}")]
[HttpGet]
public IHttpActionResult Models(string make)
{
    using (var db = new eCatEntities())
    {
        var models = db.VEHICLEs.Where(p => p.VEHI_MAKE == make.Replace("|", "/")).Select(p => new { TEXT = p.VEHI_MODEL, p.VEHI_ALPHASORT }).Distinct().OrderBy(p => p.VEHI_ALPHASORT);

        return Json(models.ToList());
    }
}

示例网址为:

http://domain:port/api/Makes
http://domain:port/api/Models/Jaguar|Daimler

2 个答案:

答案 0 :(得分:1)

扩展路由的扩展点是IHttpRoute(或其默认的具体实现HttpRoute)。处理程序是一个较低级别的对象,它不具备生成传出路由的能力(如果要在路由值中放置/,则肯定需要修复这些路径。)

public class ReplacePipeHttpRoute : System.Web.Http.Routing.HttpRoute
{
    public ReplacePipeHttpRoute(string routeTemplate, object defaults)
        : base(routeTemplate, new HttpRouteValueDictionary(defaults))
    { }

    // OPTIONAL: Add additional overloads for constraints, dataTokens, and handler

    // Converts | to / on incoming route
    public override IHttpRouteData GetRouteData(string virtualPathRoot, System.Net.Http.HttpRequestMessage request)
    {
        var routeData = base.GetRouteData(virtualPathRoot, request);

        // RouteData will be null if the URL or constraint didn't match
        if (routeData != null)
        {
            var newValues = new HttpRouteValueDictionary();
            foreach (var r in routeData.Values)
            {
                newValues.Add(r.Key, r.Value.ToString().Replace("|", "/"));
            }
            routeData = new HttpRouteData(this, newValues);
        }

        return routeData;
    }

    // Converts / to | on outgoing route (URLs that are generated from WebApi)
    public override IHttpVirtualPathData GetVirtualPath(System.Net.Http.HttpRequestMessage request, IDictionary<string, object> values)
    {
        var newValues = new Dictionary<string, object>();
        if (values != null)
        {
            foreach (var r in values)
            {
                // Encode pipe as outlined in: http://www.tutorialspoint.com/html/html_url_encoding.htm
                newValues.Add(r.Key, r.Value.ToString().Replace("/", "%7c"));
            }
        }

        return base.GetVirtualPath(request, newValues);
    }
}

用法

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.Add(
            name: "DefaultApi",
            route: new ReplacePipeHttpRoute(
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional, controller = "Vehicles" })
        );
    }
}
  

注意:您可能需要重新考虑使用网址中的竖线字符。

     

答案 1 :(得分:0)

我能够使用System.Web.Http.Filters ActionFilterAttribute实现此目的。

您的路线将如下所示:

[Route("api/Models/{make}"), MakeWebApiFilter]
[HttpGet]

和类似的过滤器:

using System.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;

public class MakeWebApiFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        object data = "";
        if(actionContext.ActionArguments.TryGetValue("make", out data))
        {
            data = data.Replace("|", "/");;
            actionContext.ActionArguments["make"] = data;
        }
    }
}

我今天遇到了这个问题,找到了许多MVC的示例,但是没有找到WebAPI的示例,直到我在这里找到答案https://damienbod.com/2014/01/04/web-api-2-using-actionfilterattribute-overrideactionfiltersattribute-and-ioc-injection/