locked
Custom Routing URL in MVC (appurl/{uniqueId}/{controller}/{action}/{id}) RRS feed

  • Question

  • User-603290994 posted

    We are trying to build an mvc application in c#. We want to change the routing config in such a way that it has uniqueId in between domain url and controller name.The uniqueId is dynamic and based on uniqueId the application will be connecting to there respective database through elastic database server.

    The problem is that mvc is considering uniqueId as a controller. When i searched i found few solutions where uniqueId is static and is already defined. Please find the link for error image: Image

    I was looking for solution like Azure devops application url. if you look at Azure devops, its multi-tenancy application serving several customers. for example: dev.azure.com/{clientname} - the client name can be anything and that will act as tenantid for elastic database pool. i tried implementing this solution, but MVC framework is not supporting such pattern and its considering {clientname} as controller name. 

    For Example: I want to build something like shown below and it is not working

    URL: portal.example.com/{uniqueId}/Account/login

    RouteConfig: routes.MapRoute( name: "Default", url: "{uniqueId}/{controller}/{action}/{id}", defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional, uniqueId= UrlParameter.Optional } );

    Please help us with this approach. Is there any other way to get this working. Thanks in advance

    Tuesday, December 25, 2018 2:31 PM

All replies

  • User1520731567 posted

    Hi nagasaibhs,

    URL: portal.example.com/{uniqueId}/Account/login

    Suppose you post to IIS.

    You need to set host name.(From localhost to portal.example.com)

    Modifying your hosts file enables you to override the domain name system (DNS) for a domain on a specific machine. 

    Please refer to this article about how to set host name.

    For example,you add:

    127.0.0.1 portal.example.com

    in hosts file.

    And then you publish the app on IIS.

    Please right click the app and click 'Edit Bindings' -> click 'Edit...',

    And then

    RouteConfig: routes.MapRoute( name: "Default", url: "{uniqueId}/{controller}/{action}/{id}", defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional, hostname = UrlParameter.Optional } );

    I saw in you code:

    you didn't set uniqueId and set a redundant variable 'hostname'.

    I suggest you could modify,like:

       routes.MapRoute(
                    name: "Default",
                    url: "{uniqueId}/{controller}/{action}/{id}",
                    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, uniqueId=5}
                );

    Both http://portal.example.com:1147/4/Home/Index and http://portal.example.com:1147/ will work.

    If you want to set 'uniqueId' to different,please refer to:

    https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/

    Using [Route] on each action.

    Hope my answer was helpful to you.

    Best Regards.

    Yuki Tao

    Wednesday, December 26, 2018 7:43 AM