Created
April 23, 2016 18:10
-
-
Save seangwright/a5b9cea0b51afc05d50e5cad48cf6075 to your computer and use it in GitHub Desktop.
BasePrefixRouteProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Http.Controllers; | |
using System.Web.Http.Routing; | |
namespace WiredViews.WIR03.Web.Api.Configuration | |
{ | |
/// <summary> | |
/// See http://www.strathweb.com/2015/10/global-route-prefixes-with-attribute-routing-in-asp-net-web-api/ | |
/// </summary> | |
public class BasePrefixRouteProvider : DefaultDirectRouteProvider | |
{ | |
private readonly string _centralizedPrefix; | |
/// <summary> | |
/// Set the base prefix in the constructor | |
/// </summary> | |
/// <param name="centralizedPrefix"></param> | |
public BasePrefixRouteProvider(string centralizedPrefix) | |
{ | |
_centralizedPrefix = centralizedPrefix; | |
} | |
/// <summary> | |
/// Generate a route using the existing route and the base prefix | |
/// </summary> | |
/// <param name="controllerDescriptor"></param> | |
/// <returns></returns> | |
protected override string GetRoutePrefix(HttpControllerDescriptor controllerDescriptor) | |
{ | |
var existingPrefix = base.GetRoutePrefix(controllerDescriptor); | |
if (existingPrefix == null) return _centralizedPrefix; | |
return string.Format("{0}/{1}", _centralizedPrefix, existingPrefix); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment