RunnX / AspNetCoreSubdomain

Simple usage lib for subdomain routing in ASP.NET Core/Framework MVC

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

Simple usage lib for subdomain routing in ASP.NET Core/Framework MVC

License:MIT License


Languages

Language:C# 98.9%Language:Ruby 0.5%Language:CSS 0.3%Language:PowerShell 0.2%Language:JavaScript 0.0%