-
-
Save malloy045/4ef6ef3efbfc7fa6bc82d70d9b17f1c3 to your computer and use it in GitHub Desktop.
Routing config for hosting both MVC and WebAPI in one .NET web project
This file contains hidden or 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
#region Namespaces | |
using Company.Project.IBusiness; | |
using Company.Project.Web.WebAPI.Framework.Authentication; | |
using System.Web.Http; | |
using System.Web.Mvc; | |
using System.Web.Routing; | |
#endregion | |
namespace Company.Project.Web.App_Start | |
{ | |
/// <summary> | |
/// Route Config | |
/// </summary> | |
public class RouteConfig | |
{ | |
/// <summary> | |
/// Registers the MVC and API routes | |
/// </summary> | |
/// <param name="mvcRoutes">The MVC route collection</param> | |
/// <param name="config">The current http configuration</param> | |
/// <param name="logger">The application logger</param> | |
/// <param name="settings">The application settings</param> | |
public static void RegisterRoutes(RouteCollection mvcRoutes, HttpConfiguration config, ILogger logger, IAppSettings settings) | |
{ | |
// NOTE: We are hosting the API and Webapps in one project so that means we have a mix of API controllers and MVC controllers. | |
// In order to get the routing correct, we use the HttpConfiguration object to map API routes, then the MVC routing table to map the MVC routes | |
#region API Controllers | |
config.Routes.IgnoreRoute("Resources", "{resource}.axd/{*pathInfo}"); | |
// This API action is intended to be public, so we pass a handler that will not require authentication | |
config.Routes.MapHttpRoute( | |
name: "LatestVersionRoute", | |
routeTemplate: "api/public/foo", | |
defaults: new { controller = "Public", action = "Foo" }, | |
constraints: null | |
); | |
// The default API Route (defined in WebApiConfig), will be "/projectName/api". | |
config.Routes.MapHttpRoute( | |
name: "DefaultApiRoute", | |
routeTemplate: "api/{controller}/{action}", | |
defaults: new { action = "DefaultAction" }, | |
constraints: null, | |
handler: new ApiAuthenticationDelegate(config, settings, logger) | |
); | |
#endregion | |
#region MVC Controller | |
// The default UI Route will be "/projectName" | |
mvcRoutes.MapRoute( | |
name: "DefaultWebUI", | |
url: "{controller}/{action}/{id}", | |
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, | |
constraints: null | |
); | |
#endregion | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment