attributerouting

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

淺唱寂寞╮ 提交于 2019-12-02 01:11:57
I am trying to change this convention based route: routes.MapRoute( "MovieByReleaseDate", "movies/released/{year}/{month}", new { controller = "Movies", action = "ByReleasedDate" }, ); to attribute route: [Route("movies/released/{year}/{month}")] but I can't see how I can define default parameters like in the first way. Steve Perkins You can use multiple [Route] attributes coupled with nullable parameters to achieve your goal. [HttpGet] [Route("movies/released/")] [Route("movies/released/{year}")] [Route("movies/released/{year}/{month}")] public string Test(int? year = 2018, int? month = 1) {

MVC5 : Attribute Routing Precedence Among Controllers

送分小仙女□ 提交于 2019-12-01 17:38:27
I am using the Attribute Routing from MVC5 in my controllers. Question: Is there a way to control attribute routing precedence among controllers? Consider the following [Route("home/{action=index}/{username?}")] public class HomeController : Controller { [Route("home/index/{username?}", Order = 1)] [Route("home/{username?}", Order = 2)] [Route("{username?}", Order = 3)] public ActionResult Index() { // ... bunch of stuff } } Base on the code above, HomeController.Index() action method should be invoked using the following requests: domain/ domain/{username} domain/home/ domain/home/{username}

Attribute vs Conventional Routing

我们两清 提交于 2019-12-01 11:39:25
Q1: So this article says attribute routing is more favourable than conventional routing for api versioning. It's not clear to me the reasons behind such claim because to me in order to support these: /api/v1/products /api/v2/products all you need to do is to define two routes: routes.MapHttpRoute("V1", "api/v1/products", new {controller = "V1Controller", action = "ListProducts"}); routes.MapHttpRoute("V2", "api/v2/products", new {controller = "V2Controller", action = "ListProducts"}); Can something share some insight? Q2: this article says one issue with conventional routing is the order of

Attribute vs Conventional Routing

▼魔方 西西 提交于 2019-12-01 09:38:43
问题 Q1: So this article says attribute routing is more favourable than conventional routing for api versioning. It's not clear to me the reasons behind such claim because to me in order to support these: /api/v1/products /api/v2/products all you need to do is to define two routes: routes.MapHttpRoute("V1", "api/v1/products", new {controller = "V1Controller", action = "ListProducts"}); routes.MapHttpRoute("V2", "api/v2/products", new {controller = "V2Controller", action = "ListProducts"}); Can

MVC AttributeRouting With GET - Returning 405 - Method Not Allowed

瘦欲@ 提交于 2019-12-01 09:19:50
I've just been working on a new controller action method and I'm a little confused why I'm seeing a 405. I have defined several GET attributed methods on my API already and they all function as expected. For an example this works fine: [GET("entries/{page}"), JsonExceptionFilter] public HttpResponseMessage GetEntries(int page) Yet my new method defined like this: [GET("search/{searchTerm}/{page}"), JsonExceptionFilter] public HttpResponseMessage Search(string searchTerm, int page) Is returning a 405 If I visit the routes.axd url on the API I can see an entry in the table like this: GET, HEAD,

Multiple GET's in Web API calling wrong action

喜你入骨 提交于 2019-12-01 09:18:24
问题 I have a Web API , that looks like the following... public class LeaguesController : ApiController { //api/Leagues/active/1 //api/Leagues/complete/1 //api/Leagues/both/1 [GET("api/Leagues/{type}/{id}")] public List<Competition> Get([FromUri]int id, [FromUri]CompetitionManager.MiniLeagueType type) { return CompetitionManager.GetUsersMiniLeagues(id, true, type); } //api/Leagues/GetMiniLeagueTable/3 [GET("api/Leagues/GetMiniLeagueTable/{id}")] public List<SportTableRow> GetMiniLeagueTable(

MVC AttributeRouting With GET - Returning 405 - Method Not Allowed

梦想的初衷 提交于 2019-12-01 06:40:31
问题 I've just been working on a new controller action method and I'm a little confused why I'm seeing a 405. I have defined several GET attributed methods on my API already and they all function as expected. For an example this works fine: [GET("entries/{page}"), JsonExceptionFilter] public HttpResponseMessage GetEntries(int page) Yet my new method defined like this: [GET("search/{searchTerm}/{page}"), JsonExceptionFilter] public HttpResponseMessage Search(string searchTerm, int page) Is

Multiple routes assigned to one method, how to determine which route was called?

99封情书 提交于 2019-12-01 05:14:14
I am working on a small ASP.NET MVC project at the moment. The project was released a few month ago. But changes should be implemented for usability and SEO reasons now. I decided to use attribute routing to create clean URLs. At the moment a product page is called by: hostname.tld/Controller/GetArticle/1234 I defined a new Route like this: [Route("Shop/Article/{id:int}/{title?}", Name = "GetArticle", Order = 0)] public ActionResult GetArticle(int id, string title = null) { // Logic } Everything works fine, but because of backwards compatibility and SEO reasons, the old route should be still

How to find the right route in a RouteCollectionRoute?

馋奶兔 提交于 2019-11-30 18:16:30
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() { .... } [Route("foo")] [HttpPost] public ActionResult Post() { .... } } Then in order to test which route matches a particular request, I call routes.GetRouteData . I get a System.Web.Routing.RouteData that contains the route as well as values that should say which controller and action are matched. The problem is that this route is now an instance of RouteCollectionRoute this

ASP.NET Help Pages default home page?

假装没事ソ 提交于 2019-11-30 10:59:24
问题 I want to go to http://myserver and be able to get Help Pages as the default home page, so the first thing a guest to http://myserver should see is the Help Page. I have a default route set up like this: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } Then I have my Help Page