Giter VIP home page Giter VIP logo

aspnetcoresubdomain's Introduction

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

aspnetcoresubdomain's People

Contributors

fugaz1 avatar mariuszkerl avatar mkerl-neurosys avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.