Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Currently I'm working on an asp.net mvc project. I have done an attribute routing and it's working well. The code is given below.
Now I'm thinking of keeping the Route values on a table and set it to relevant actions.

For example currently for the action TestOne I've set "Test_Test_One" as a route.
What I want to do is taking that route value from a database table and set it to the TestOne action.

Is it possible to do? Can someone help me on this? Thanks in advance.

What I have tried:

[RoutePrefix("")]
public class TestController : Controller
{

	public ActionResult Index(int page)
        {
            switch(page)
            {
                case 1: return RedirectToAction("TestOne", "Test");               
                default: return View();
            }
            
        }

	[Route("Test_Test_One")]
        public ActionResult TestOne()
        {
            return View();
        }
}
Posted
Updated 26-Nov-19 20:14pm

1 solution

What you are looking for is to be done on the runtime, and these attributed at mapped at compile-time. You will not be able to modify them after your app has been built.

One way to solve this would be to use a dynamic route and then process the actual URI on runtime and execute the controller/action based on that.

Dynamic controller routing in ASP.NET Core 3.0 | StrathWeb. A free flowing web tech monologue.[^]

Most of the dynamic path mapping techniques would also fail since your data comes from a database. So unless your database is used to generate the code and routes in the application, these techniques would fail. You would need to use a wildcard to catch all the requests and then pass the request to the appropriate controller and action. Yep, things like RedirectToAction() would come in use here. This thread on SO talks about this approach, c# - Dynamic Routes from database for ASP.NET MVC CMS - Stack Overflow[^]

Handle requests with controllers in ASP.NET Core MVC | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
Krishnananthan Vijayaretnam 2-Dec-19 8:22am    
Thank you for your answer. I will try this and mark as answer if it works for my scenario.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900