Skip to content
Simple usage lib for subdomain routing in ASP.NET Core/Framework MVC
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.vscode Constraints as a parameter Jun 27, 2018
src v1.1.0 release notes Jul 2, 2018
test Moved project and some refactoring Jul 2, 2018
.gitignore
LICENSE Initial commit Aug 1, 2017
README.md Update README.md Jan 27, 2018

README.md

logo

NuGet

ASP.NET Subdomain Routing

Goal of that lib is to make subdomain routing easy to use in asp net core mvc applications. Normally you would use some custom route for some special case scenario in your app. This should solve most of issues while using subdomain routing. Inspired by couple of already existing libraries around the web which handle routing in some degree this should meet requirements:

  1. Register subdomain routes as you would do with normal routes.
  2. Make links, forms urls etc. in views as you would do with helpers in your cshtml pages.
  3. Catch all route values in controller.

Continuous Integration

Build server Build status Unit tests Integration tests Functional tests
AppVeyor Build status Build status Build status Build status

Wiki

https://github.com/mariuszkerl/AspNetCoreSubdomain/wiki

Setup

Startup.cs

Your application have to be aware of using subdomains. Important thing is to use method AddSubdomains() before AddMvc()

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    //...
    services.AddSubdomains();
    services.AddMvc();
}

You configure your routes just like standard routes, but you cannot use standard MapRoute methods. That will be explained later in wiki. Use MapRoute method from this lib extensions method which accepts hostnames as a parameter.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    var hostnames = new[] { "localhost:54575" };
    
    app.UseMvc(routes =>
    {
        routes.MapRoute(
            hostnames,
            "NormalRoute",
            "{action}",
            new { controller = "SomeController" });

        routes.MapSubdomainRoute(
            hostnames,
            "SubdomainRoute",
            "{controller}", //that's subdomain parameter, it can be anything
            "{action}",
            new { controller = "Home", action = "Action1" });
    )};
}

Usage

Your .cshtml files

Goal of that library is not only catching routes for subdomain but also generating links to actions while persisting standard razor syntax. Helper below will generate url <a href="http://home.localhost:54575">Hyperlink example</a>. Route named SubdomainRoute should catch that link.

@Html.ActionLink("Hyperlink example", "Action1", "Home")

Controller

Big advantage of library is you can catch all route values with controller.

//HomeController.cs
public IActionResult Action1()
{
    //code
}

Having url http://home.localhost:54575/ will invoke Action1 method in Home controller.

Running samples project

https://github.com/mariuszkerl/AspNetCoreSubdomain/wiki/Running-samples-project

You can’t perform that action at this time.