Questions tagged [attributerouting]

Define your routes using attributes on action methods in ASP.NET MVC and Web API.

NOTE: Attribute Routing support is currently built into Web API. You can read more information about it here: http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

371 questions
11
votes
3 answers

MVC 5 AttributeRouting Catch All

How do I create a catch all route with the new Attribute routing in MVC I tried this: [Route("{pagenode}", Order = 999)] But when I have a named route like [Route("contact"] I get the "Multiple controller types were found that match the URL. This…
Ivo
  • 3,406
  • 4
  • 33
  • 56
11
votes
2 answers

How to find the right route in a RouteCollectionRoute?

I am testing ASP MVC routes. I am having an issue with attribute routes in ASP MVC 5.1 When I have a controller like this: public class FooController : Controller { [Route("foo")] [HttpGet] public ActionResult Get() { .... …
10
votes
1 answer

How do I get an Mvc 5 Controller's ActionDescriptor when using Attribute Routing?

I am trying to get get an ActionDescriptor for an action on a controller that uses Attribute Routing, however it is always null. var controllerDescriptor = new ReflectedControllerDescriptor(controllerType); var actionDescriptor = …
9
votes
2 answers

How to set the default area in ASP.NET Core?

I am using areas in my ASP.NET Core 3.1 application (MVC). Now I want all requests without an explicit area to go to the "Main" area by default. This is how I currently set up my endpoint routing: app.UseEndpoints(endpoints => { // 1 …
Ingmar
  • 1,525
  • 6
  • 34
  • 51
9
votes
3 answers

Construct url in view for Web Api with attribute routing

How can I get the url from web api in my view? Example (from the msdn-blog): [RoutePrefix("reviews")] public class ReviewsController : ApiController { // eg.: /reviews [Route] public IHttpActionResult Get() { ... } // eg.:…
Team-JoKi
  • 1,766
  • 3
  • 15
  • 23
9
votes
5 answers

Specify default controller/action route in WebAPI using AttributeRouting

How does one set the default controller to use when using AttributeRouting instead of the default RouteConfiguration that WebAPI uses. i.e. get rid of the commented code section since this is redundant when using AttribteRouting public class…
Abhijeet Patel
  • 6,562
  • 8
  • 50
  • 93
8
votes
4 answers

Override controller name in ASP.NET Core

Similar to this question, but for the new ASP.NET Core. I can override an action's routing name: [ActionName("Bar")] public IActionResult Foo() { Can I do that for a controller, using attribute routing? [?("HelloController")] public SomeController…
grokky
  • 8,537
  • 20
  • 62
  • 96
8
votes
4 answers

Is there a way to have a RoutePrefix that starts with an optional parameter?

I want to reach the Bikes controller with these URL's: /bikes // (default path for US) /ca/bikes // (path for Canada) One way of achieving that is using multiple Route Attributes per Action: [Route("bikes")] [Route("{country}/bikes")] public…
Frank van Eykelen
  • 1,926
  • 1
  • 23
  • 31
8
votes
1 answer

Route precedence with attribute routing

In older MVC versions, with the AttributeRouting library, I can have multiple routes and specify a precedence, so the most appropriate is picked when generating URLs: [Route("", ActionPrecedence = 1)] [Route("city/{citySlug}", ActionPrecedence =…
CMircea
  • 3,543
  • 2
  • 36
  • 58
8
votes
1 answer

AttributeRouting not working with HttpConfiguration object for writing Integration tests

I'm creating some integration tests following the ideas outlined here: http://www.strathweb.com/2012/06/asp-net-web-api-integration-testing-with-in-memory-hosting/ When I try to register routes from a hand crafted HttpConfiguration object I'm…
7
votes
1 answer

How to determine Route Prefix programmatically in asp.net mvc?

I wanted to provide some URL separation for my public/anonymous controllers and views from the admin/authenticated controllers and views. So I ended up using entirely Attribute Routing in order to take more control of my URLs. I wanted my public…
Jiveman
  • 1,022
  • 1
  • 13
  • 30
7
votes
1 answer

How to redirect to route using custom attribute routing in MVC5

I'm not sure if what I'm attempting to do is valid as I'm a relative newbie to the C# / ASP.NET / MVC stack. I have a controller action like this in ModelController.cs //Get [Route("{vehiclemake}/models", Name =…
7
votes
1 answer

What is the benefit of adding "routes.MapMvcAttributeRoutes()" to RegisterRoutes()?

According to the article "What's New in ASP.NET MVC5", you must add this to your RegisterRoutes() method in order to use Attribute Routing: routes.MapMvcAttributeRoutes(); ...so that the method is now: public static void…
6
votes
2 answers

how to add default parameters to attribute routes in asp.net mvc

I am trying to change this convention based route: routes.MapRoute( "MovieByReleaseDate", "movies/released/{year}/{month}", new { controller = "Movies", action = "ByReleasedDate" }, ); to attribute…
6
votes
1 answer

ASP Net Core Attribute routing and double forward slash

As pointed out here, having a double slash in a URL is valid. I have an ASP Net Core project that uses attribute routing, a controller named GroupController for handling operations on Groups and an action for PUTting RuleParts of a group, specified…
1 2
3
24 25