How to set different routes for the same Controller method with different parameters?

Viewed 1810

I have the ReportController that llooks like this:

public IActionResult ReportDetails(int? reportId){
    ....
}

and

public IActionResult ReportDetails(int? reportId, bool ? approved) {
    ....
}

and I tried to do some routing, bu it failed. Actual code written in Startup.cs:

app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "ReportDetailsValidation",
                    template: "descriptionValidation",
                    defaults: new { controller = "Report", action = "ReportDetails", reportId ="{reportId}", approved = "{approved}" }
                    );

                routes.MapRoute(
                    name: "ReportDetails",
                    template: "description",
                    defaults: new { controller = "Report", action = "ReportDetails"} 
                    );

                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

As I have seen I need to match the following URL's:
/Report/ReportDetails?reportId=7 for the first and
/Report/ReportDetails?reportId=7&approved=True for second

4 Answers
Related