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

  • Question

  • 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: https://ibb.co/y89KVN8

    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





    • Moved by CoolDadTx Wednesday, December 26, 2018 4:08 PM ASP.NET related
    • Edited by NagasaiBHS Friday, December 28, 2018 12:50 PM
    Tuesday, December 25, 2018 10:13 AM

All replies

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

    MVC forum is at below link.https://forums.asp.net/

    Tuesday, December 25, 2018 10:27 AM
  • Yes, doing it the way you are doing it should work. That is, adding a route in the form "{uniqueId}/{controller}/{action}/{id}" is a good approach. If it thinks that the uniqueid is a controller name, it might be because you have a previous MapRoute with a different mapping that has the controller in the first place. The order of the MapRoute's is important because they are processed in order, and the first one that can be matched is used. Another reason for the route to fail could be that your uniqueid contains a "/".
    • Proposed as answer by Cherkaoui.Mouad Wednesday, December 26, 2018 3:45 PM
    • Unproposed as answer by Cherkaoui.Mouad Wednesday, December 26, 2018 3:46 PM
    • Proposed as answer by Cherkaoui.Mouad Wednesday, December 26, 2018 3:50 PM
    Tuesday, December 25, 2018 5:02 PM
  • Hi Alberto, 
    Thanks for reply. This is the only route configured in the application and uniqueId is a string with no special characters in it (Just a plain text).
    • Proposed as answer by Cherkaoui.Mouad Wednesday, December 26, 2018 4:13 PM
    • Unproposed as answer by Cherkaoui.Mouad Wednesday, December 26, 2018 4:17 PM
    Tuesday, December 25, 2018 5:20 PM
  • Hi NagasaiBHS,

    Thank you for posting here.

    According your post, your question is more relevant to the MVC,  Please post in the following forum.

    https://forums.asp.net/1146.aspx/

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    Best regards,

    Jack J Jun.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 26, 2018 2:06 AM
  • Hello, 

    did you have any declared RouteAttribute? if so when they're mapped? before or after your custom route, this is the only reason that will affect the MvcRouteHandler behavior.

    Asp.Net Mvc Pipeline

    Hope it helps,

    Good Coding;


    Wednesday, December 26, 2018 4:21 PM
  • Hi,

    Only this route config is declared in the application configuration.

    Friday, December 28, 2018 12:52 PM
  • Hi, 

    then you should debug your routing, here is an article about the topic ASP-Net-MVC-Debugging-your-routes.

    Note:

    I think you should post your question in the Asp.Net forums as suggested by Jack J Jun, thus you'll have more answers since contributors are all Asp.Net users and specialists.

    Saturday, December 29, 2018 4:41 PM