Previously in ASP.NET MVC
<routes>
<add name="VehicleDetailRoute" url="TwoWheeler/VehicleDetail/{id}">
<defaults controller="Integration" action="VehicleDetail" id="Optional" />
</add>
</routes>
public void RegisterRoutes(RouteCollection routes)
{ //Fill the route table section
RouteSection routesTableSection = GetRouteTableConfigurationSection();
if (routesTableSection == null || routesTableSection.Routes.Count <= 0)
return;
for (int routeIndex = 0; routeIndex < routesTableSection.Routes.Count; routeIndex++)
{
//base on different route, we have created dynamic routes and add in to
var routeElement = routesTableSection.Routes[routeIndex];
var route = new Route(
routeElement.Url,
GetDefaults(routeElement),
GetConstraints(routeElement),
GetDataTokens(routeElement),
GetInstanceOfRouteHandler(routeElement));
routes.Add(routeElement.Name, route);
}
}
Here, Customer see TwoWheeler/VehicleDetail/ on browser but internally it's consider controller as Integration and action as "VehicleDetail"
Now in .Net Core, we were created JSON file and want to read. (want migrate into .Net Core 2.0 from ASP.NET MVC). You can change the format or JSON if required.
{
"routes": {
"VehicleDetailRoute": {
"url": "TwoWheeler/VehicleDetail/{id}",
"controller": "Integration",
"action": "VehicleDetail",
"id": "Optional"
}
}
}
- url : Showing on browser
- controller : Original Controller
- action : original Action
- VehicleDetailRoute : route name
So far, i have read configuration in Startup file
public Startup(IConfiguration configuration)
{
var configurationBuilder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("routes.json", optional: false, reloadOnChange: true);
Configuration = configurationBuilder.Build();
}
Added services.AddRouting(); in ConfigureServices
In Configure method, added app.UseStaticFiles();
Build Routing
var routeBuilder = new RouteBuilder(app);
routeBuilder.MapGet("{route}", context =>
{
var routeMessage = Configuration.AsEnumerable()
.FirstOrDefault(r => r.Key == context.GetRouteValue("route")
.ToString())
.Value;
var defaultMessage = Configuration.AsEnumerable()
.FirstOrDefault(r => r.Key == "default")
.Value;
var response = (routeMessage != null) ? routeMessage : defaultMessage;
return context.Response.WriteAsync(response);
});
app.UseRouter(routeBuilder.Build());
Question : Base on URL enter by customer, i want to redirect particulate action. Want to execute below method with dynamic input
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Integration}/{action=VehicleDetailOnline}/{id?}");
});
Checked below reference : radu-matei.com -- c-sharpcorner -- StackOverflow