Skip to content

Instantly share code, notes, and snippets.

@mrsheepuk
Created October 21, 2015 12:26
Show Gist options
  • Save mrsheepuk/10046e23727d4eb86ca2 to your computer and use it in GitHub Desktop.
Save mrsheepuk/10046e23727d4eb86ca2 to your computer and use it in GitHub Desktop.
Prefixing all MVC6 / Web API routes with a configured value
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc.ApplicationModels;
using Microsoft.AspNet.Mvc;
namespace MrSheep.Shared
{
public class AutoPrefixingRouteConvention : IApplicationModelConvention
{
private string m_baseUrl = "";
public AutoPrefixingRouteConvention(string baseUrl)
{
this.m_baseUrl = baseUrl;
}
public void Apply(ApplicationModel application)
{
var centralPrefix = new AttributeRouteModel(new RouteAttribute(m_baseUrl));
foreach (var controller in application.Controllers)
{
if (controller.AttributeRoutes.Any())
{
for (var i = 0; i < controller.AttributeRoutes.Count; i++)
{
controller.AttributeRoutes[i] = AttributeRouteModel.CombineAttributeRouteModel(
centralPrefix, controller.AttributeRoutes[i]);
}
}
else
{
controller.AttributeRoutes.Add(centralPrefix);
}
}
}
}
}
{
"MrSheepWeb": {
"BaseUrl": "/base/"
}
}
using System;
using Microsoft.AspNet.Builder;
using Microsoft.Framework.DependencyInjection;
using Microsoft.AspNet.Hosting;
using Microsoft.Framework.Configuration;
using Microsoft.Dnx.Runtime;
using Microsoft.AspNet.Http.Features;
using Microsoft.Net.Http.Server;
using Microsoft.Framework.OptionsModel;
using MrSheep.Shared;
namespace MrSheep.WebExample
{
public class Startup
{
public IConfiguration Configuration;
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
Configuration = new ConfigurationBuilder()
.SetBasePath(appEnv.ApplicationBasePath)
.AddJsonFile("config.json")
.AddJsonFile($"config.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables()
.Build();
}
public void ConfigureServices(IServiceCollection services)
{
// Configuration
services.Configure<MrSheepWebConfig>(Configuration.GetSection("MrSheepWeb"));
// Calculate the api base URL - need to trim the leading slash if present.
var apiBaseUrl = Configuration["MrSheepWeb:BaseUrl"];
if (apiBaseUrl.StartsWith("/"))
{
if (apiBaseUrl.Length > 1) apiBaseUrl = apiBaseUrl.Substring(1);
else apiBaseUrl = "";
}
// Add an auto-prefixing convention to add the api base URL to any attribute routes
services.AddMvc(opt =>
{
if (apiBaseUrl != "")
opt.Conventions.Insert(0, new AutoPrefixingRouteConvention(apiBaseUrl));
});
}
public void Configure(IApplicationBuilder app, IOptions<MrSheepWebConfig> config)
{
app.UseIISPlatformHandler();
// Serve images etc - need to use the base URL without a trailing /
var baseUrl = config.Value.BaseUrl;
if (baseUrl.EndsWith("/")) baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
app.UseStaticFiles(baseUrl);
// Serve views and the API.
app.UseMvc();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment