asp.net-web-api-routing

Route parameter with slash “/” in URL

时光毁灭记忆、已成空白 提交于 2019-12-28 05:56:09
问题 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")] Update: I know there are few options to solve the issue by refactoring the code so please do not offer a solution something like: change the route template to [Route("orders/customers/{orderdate

Why is my Web API method with double args not getting called?

↘锁芯ラ 提交于 2019-12-28 04:25:27
问题 I have a Web API method that takes two double args: Repository interface: public interface IInventoryItemRepository { . . . IEnumerable<InventoryItem> GetDepartmentRange(double deptBegin, double deptEnd); . . . } Repository: public IEnumerable<InventoryItem> GetDepartmentRange(double deptBegin, double deptEnd) { // Break the doubles into their component parts: int deptStartWhole = (int)Math.Truncate(deptBegin); int startFraction = (int)((deptBegin - deptStartWhole) * 100); int deptEndWhole =

How do I specify the class instance I want my Controller's constructor to receive (when working with Web API, DI, and Castle Windsor)?

不想你离开。 提交于 2019-12-25 04:33:31
问题 Perhaps my questions here and here are not clear enough, so I'll try to decrease the verbosity while not reducing the clarity. Say my Controller uses DI (you can verbalize it "in your head" - you don't have to utter it out loud); so it could look like this: private readonly IDepartmentRepository _deptsRepository; public DepartmentsController(IDepartmentRepository deptsRepository) { if (deptsRepository == null) { throw new ArgumentNullException("deptsRepository is null"); } _deptsRepository =

Differentiating Get Method Containing Query String in ASP.NET WEB API

£可爱£侵袭症+ 提交于 2019-12-25 04:19:33
问题 I have a ASP.NET Web Api Containing following Get Methods: [RoutePrefix("api/contact")] public class ContactController : ApiController { private ContactContext db = new ContactContext(); public IEnumerable<Contact> Get() { return db.Contacts.AsEnumerable(); } [Route("{id:int}")] public Contact Get(int id) { var result = db.Contacts.Find(id); if (result == null) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound)); } return result; } public IEnumerable<Contact>

Why is a complex string (stringified json data) not accepted as a POST parameter to a Web API method, but a simple string is?

核能气质少年 提交于 2019-12-24 14:53:16
问题 I want to send a bunch of data, converted to json, as a string, to a Web API POST method. I can send a simple string just fine, but when I try to send stringified json data, the method is not even reached - apparently the complex string is not viewed as a valid string value or something. This works, when passing "randomString" from the client: Web API [Route("{unit}/{begindate}/{enddate}/{stringifiedjsondata}")] [HttpPost] public void Post(string unit, string begindate, string enddate, string

C# .net core web api post parameter always null

橙三吉。 提交于 2019-12-24 09:02:28
问题 So this is driving me nuts. I'm doing something very simple sending POST request to my web api. The endpoint is set up as follows: [HttpPost] [Route("locations")] public async Task<IActionResult> PostLocations([FromBody]IEnumerable<Location>locations) and I'm making the call as follows: http://localhost:60254/api/Fetch/locations With the body { "Locations": [ { "LocationId": "111", "ProductId": 110, "Sku": "11131-LJK" } ] } And header: content-type: application/json now again, this is VERY

Multiple Get actions with different attribute routing?

时光怂恿深爱的人放手 提交于 2019-12-24 07:39:48
问题 If I design my controller in such a way: public class ItemController : ApiController { [HttpGet] [RoutePrefix("item/dosomething")] public void DoSomething(Item item) { } [HttpGet] [RoutePrefix("item/dosomethingnicer")] public void DoSomethingNicer(Item item) { } [HttpGet] [RoutePrefix("item/dosomethingelse")] public void DoSomethingElse(Item item) { } } Would this work? 回答1: I would expect a structure more akin to this: [RoutePrefix("item")] public class ItemController : ApiController {

Multiple optional parameters web api attribute routing

☆樱花仙子☆ 提交于 2019-12-23 15:24:08
问题 Hi guys i am new to attribute routing and not sure if this is even possible. What i have is an attribute route which works fine like this [HttpGet] [Route("GetIssuesByFlag/{flag:int=3}")] public IEnumerable<IssueDto> GetIssuesByFlag(int flag) now however i want to add some extra optional parameters to narrow down my search so i want to add 2 extra optional parameters. what i have tried: [HttpGet] [Route("GetIssuesByFlag/{flag:int=3?}/{categoryId:int?}/{tagIds?}")] public IEnumerable<IssueDto>

How to use the same route with different parameter types?

和自甴很熟 提交于 2019-12-23 12:49:28
问题 I have an Api controller with two different actions that take different parameter types. // GET: users/sample%40email.com [Route("users/{emailAddress}")] public IHttpActionResult GetUser(string emailAddress) // GET: users/1D8F6B90-9BD9-4CDD-BABB-372242AD9960 [Route("users/{reference}")] public IHttpActionResult GetUserByReference(Guid reference) Problem is multiple actions are found matching when I make a request to either. Looking at other answers I thought I needed to setup routes in the

How to get controller name when Web API versioning with routing attribues

。_饼干妹妹 提交于 2019-12-23 11:58:58
问题 I need to get the controller name from my route and this I can do if using standard routing code in WebApiConfig. However, if I am using routing attributes it starts to get a little difficult, especially when trying to version. Example: If I call an api/terms/bonuses and I have a BonusController and BonusV2Controller and a BonusV3Controller, this code returns the latest controller version 3. That's ok, I can live with that returning the latest and greatest version as a default. var