Controller level parameter with ASP.NET MVC attribute routing

Murray

Is there any way to add a parameter to the Controller Routing attribute?

something like:

[Route("controller/{id}/"]

public class Controller {
   public Controller(string id) { /*..*/ }

   [HttpGet]
   public ActionResult Get() { /*..*/ }
}
Josh

The [FromRoute] attribute on your controller method arguments should get you what you need.

Example

[Route("api/Test/{testId}")]
public class TestController: ControllerBase 
{
    [HttpGet]
    [Route("echo")]
    public void TestMethod([FromRoute]string testId)
    {
        return testId;
    }

}

Also see this S.O. for more information.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Send Html as parameter from javascript to asp.net mvc controller

ASP.Net MVC @Url.Action() routing to the wrong controller

Asp.net mvc routing without a controller or action name

ASP.NET MVC - Attribute Routing not finding area view

ASP.NET5 MVC 6 routing with optional parameter / default parameter value

Passing custom parameter in custom attribute - ASP.NET MVC

How to generate URL for the action with attribute routing in Asp.Net MVC

How to call controller action with a routeValue parameter in ASP.NET MVC

ASP.NET MVC Web API Attribute Routing With overloads

asp mvc 5 Attribute routing not firing

Repository Parameter in controller asp.net mvc

mvc attribute routing with optional parameter

How to convert milliseconds to DateTime in ASP.NET MVC controller parameter

ASP.NET MVC Attribute routing

ASP.net MVC routing

.net WebApi attribute routing with a collection parameter

Tow way for call a action in asp.net mvc with route attribute - Routing In asp.net mvc

ASP.NET MVC Attribute-Routing with Null Values

asp.net mvc routing, with area as parameter

Asp.net MVC Routing w/ string parameter

How routing works with default controller and action in asp.net mvc

Asp.net MVC 5 Attribute Routing Constraint

Validating and passing controller-level parameters with ASP.NET MVC attribute routing

How can I exclude a controller from ASP.NET Core 3 Attribute Routing

How can i configure attribute based routing on startup file instead of using attribute on controller action in asp.net core mvc

ASP.NET Core MVC routing parameter in Home/Index

ASP.NET MVC Routing not working for a specific controller

ASP.NET MVC Attribute routing parameter issue

Default attribute routing not worrking in ASP.NET MVC