FAQ Database Discussion Community


Attributed Routing with multiple parameters in Web API 2

routes,asp.net-web-api2,attributerouting
I am trying attributed routing with Web API 2. I have defined a route prefix and I have two methods. The first one works but the second one fails [RoutePrefix("api/VolumeCap")] public class VolumeCapController : ApiController { [Route("{id:int}")] public IEnumerable<CustomType> Get(int id) { } [Route("{id:int}/{parameter1:alpha}")] public CustomType Get(int id, string parameter1)...

MVC Routing optional string parameter and ugly urls

asp.net-mvc,attributerouting
I am using mvc 5.2.3 on 4.5.1 and want these urls /Nike/Shoes /Nike/Shoes/Running /Nike/Shirts/2002342345 /Nike/Shoes/Running/98765432234 My controller has: [HttpGet] [Route("{brand}/{category}/{subcategory}/{page:int?}", Name="ProductIndex")] public ActionResult Index(string brand, string category, int? page, string subcategory = "") and gives /Nike/Shoes/Running but no /Nike/Shoes but /Products?brand=Nike&category=Shoes When I add [HttpGet] [Route("{brand}/{category}/{page:int?}")] public ActionResult Index(string brand,...

Using '.' in Web Api using Attribute Routing is giving 404 error

asp.net-web-api,routes,asp.net-web-api2,asp.net-web-api-routing,attributerouting
I am using Routing Attribute to try to send a string via url to my web Api. I have a Route defined as [Route("Search/{searchText}")] I am expecting a string value as searchText. It seems to be working fine but I am getting an error if there is a '.' in...

WebAPI route regex with optional parts

regex,asp.net-web-api,attributerouting
I want to map a WebAPI action method to urls on the format api/v2/l8n/{cultureCode}, but to avoid route collisions with other methods, I need to constrain the cultureCode parameter to only values matching the regex ^\w{2}(?:-\w{2})?$, i.e. sv and en-GB, but not hello. I have a RoutePrefix attribute on the...

Html.Action with attribute routing

asp.net-mvc,razor,attributerouting
My route attribute is like below [RouteArea("Settings")] [RoutePrefix("Stores")] public class SettingsMallStoresController : Controller { [HttpGet] [Route("ExistingStores/{Permalink}")] public ActionResult GetExistingStoreBranchesForSelectedHO(string permalink) { return View("ExistingStoreBranches"); } } I am trying to call this action from View using @html.action @Html.Action("Settings/Stores/ExistingStores/{Permalink}",new{permalink=Somevalue}) Can't make it work in anyway.Please help...

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

c#,asp.net-mvc,asp.net-mvc-5,asp.net-mvc-routing,attributerouting
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 URLs to start with "~/Admin/etc." while my public URLs would...

Route parameter with slash “/” in URL

asp.net-web-api,routing,asp.net-web-api2,asp.net-web-api-routing,attributerouting
I know you can apply a wildcard in the route attribute to allow / such as date input for example: [Route("orders/{*orderdate}")] The problem with wildcard is only applicable to the last paramter in URI. How do I solve the issue if want to have the following URI: [Route("orders/{orderdate}/customers")] ? EDIT:...