5

The default routing for my ASP.NET Core is as defaulted by VS2015 to be:

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

When I make my request URL /my-controller/my-special-view it indeed calls the method in my my-control that is defined as follows with the id parameter equal to my-special-value

public IActionResult my-controller(string id) {...

I'm trying to the same thing with attribute routing. I define the attribute as follows:

[Route("my-controller/{id}")]
public IActionResult my-controller(string id) {...

I get an error as follows:

InvalidOperationException: The constraint entry 'attributeOfInterest' - 'string' on the route 'CacheTagHelper/{attributeOfInterest:string}' could not be resolved by the constraint resolver of type 'DefaultInlineConstraintResolver'.

I'm not experienced with attritube routing and do not understand why the above to controller methods don't give me the same results.

1 Answer 1

6

The default routing template is {controller=Home}/{action=Index}/{id?} so you can follow the template in your controller action

the my-controller will be your inherited class from controller class and the IActionResult match with your action method, if you specific the attribute in your put to route the {Id} , in your method parameter should also specific the {Id}, in routing, you also need to specific the http verb actions(GET,POST, PUT etc)

in your case would be

public class my-controller : Controller
{
   [HttpGet("{id}")]
   public IActionResult my-special-view(String id) { ... }
}
Sign up to request clarification or add additional context in comments.

2 Comments

What about your other answer with "FromRoute"? Specifically, I'm looking for using the route attribute so I can make my template in my controller and not the startup class.
Using [HttpGet("{id}")] actually caused me problem. For me, [HttpGet] alone worked well. You may want to read here: learn.microsoft.com/en-us/aspnet/core/mvc/controllers/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.