0

I have an ASP.NET Core 3 MVC web app and I have a couple of controllers.

I am trying to form my links to access these controllers and I am unsure what I am doing wrong.

Controller names: Exhibitors, DemoQueue

Each controller has an Index action which takes 2 int type parameters

public IActionResult Index(int eventId, int companyId)

And here is my relevant Startup.cs code

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
    endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
});

So it is my understanding that I can browse to these 2 actions by these urls:

Exhibitors/1/2
DemoQueue/3/4

But it seems I have to use this longwinded value:

Exhibitors/Index?eventId=1&companyId=2

Is there a way to set a route to enable me to go to [controller]/id/id ? But go to a different controller e.g. Exhibitors or DemoQueue

3
  • 1
    You can always use attributes on controllers/actions to customise the routing engine, you don't need to follow the default route. learn.microsoft.com/en-us/aspnet/core/fundamentals/… Commented Oct 19, 2020 at 19:39
  • I read that attribute routes should be left for rest apis ? Commented Oct 19, 2020 at 19:50
  • 2
    That's what you're doing. The only difference between "Rest APIs" and "Websites" is that websites mostly use GET verbs and return HTML instead of json/xml. It's the same thing under the hood. Commented Oct 19, 2020 at 19:52

2 Answers 2

3

You didn't have your custom route template defined. In your startup.cs, all you had was the default routing template.

To map requests like Exhibitors/1/2 and DemoQueue/3/4 to their corresponding controllers Index method, you need to add the following before the default conventional routing template:


app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "exhibitors-custom",
        pattern: "exhibitors/{eventId:int}/{companyId:int}",
        defaults: new { controller = "exhibitors", action = "index" }
    );

    endpoints.MapControllerRoute(
        name: "demoqueue-custom",
        pattern: "demoqueue/{eventId:int}/{companyId:int}",
        defaults: new { controller = "demoqueue", action = "index" }
    );

    endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
});

enter image description here

enter image description here


Mixed routing

You can mix the use of conventional routing and attribute routing, but it's typical to use conventional routes for controllers returning HTML for browsers, and attribute routing for controllers serving RESTful APIs.

Reference: https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-3.1#mixed-routing-attribute-routing-vs-conventional-routing

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. This helped me figure out how to setup an endpoint that was /Report/001
1

You can change your code to these

public class ExhibitorsController:Controller
[Route("~/Exhibitors/{eventId}/{companyId}")]
public IActionResult Index (int eventId, int companyId)

and

public class DemoQueueController:Controller
[Route("~/DemoQueue/{eventId}/{companyId}")]
public IActionResult Index (int eventId, int companyId)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.