Web API HttpGetAttribute errors

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Web API HttpGetAttribute errors



I'm making an API for accessing a SQL Server database. I've written the models and classes that I need, so now need to create a controller for joining everything up.



My controller code at the moment is


using Microsoft.AspNetCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EPOSDatabase_API.DataProvider;
using EPOSDatabase_API.Models;
using System.Web.Http;

namespace EPOSDatabase_API.Controllers


[Route("api/[controller]")]
public class CategoryController : ApiController

private ICategoryDataProvider categoryDataProvider;

public CategoryController(ICategoryDataProvider categoryDataProvider)

this.categoryDataProvider = categoryDataProvider;


[HttpGet]
public async Task<IEnumerable<Category>> Get()

return await this.categoryDataProvider.GetCategories();


[HttpGet("id")]
public async Task<Category> Get(int Category_ID)

return await this.categoryDataProvider.GetCategory(Category_ID);


[HttpPost]
public async Task Post([FromBody]Category category)

await this.categoryDataProvider.AddCategory(category);


[HttpPut("id")]
public async Task Put(int Category_ID, [FromBody]Category category)

await this.categoryDataProvider.UpdateCategory(category);


[HttpDelete("id")]
public async Task Delete(int Category_ID)

await this.categoryDataProvider.DeleteCategory(Category_ID);





However for the lines



[HttpGet("id")], [HttpPut("id")] and [HttpDelete("id")] I get an error


[HttpGet("id")]


[HttpPut("id")]


[HttpDelete("id")]



'HttpGet/Put/DeleteAttribute' does not contain a constructor that takes 1 argument



Why is this error occurring, am I missing a using reference, perhaps?


using





tried to import Microsoft.AspNetCore.Mvc instead? seems conflicted with System.Web.Mvc..
– Bagus Tesa
Aug 8 at 14:12


Microsoft.AspNetCore.Mvc


System.Web.Mvc





using Microsoft.AspNetCore; and using System.Web.Http; – You are mixing ASP.NET Core and classic ASP.NET MVC. That’s a very bad idea and will break. Choose just one.
– poke
Aug 8 at 14:13


using Microsoft.AspNetCore;


using System.Web.Http;





@BagusTesa Yes, I tried that, but when trying it is said "The type or namespace 'Mvc' does not exist in the namespace Microsoft.AspNetCore"
– Harambe
Aug 8 at 14:13





For MVC (not Core) you can use a Route attribute to specify route parameters.
– Crowcoder
Aug 8 at 14:15





If you have started with an ASP.NET MVC project (not Core), then you’re best off starting from scratch and manually moving over your logic. Some things translate 1-to-1 but a lot of things will not, starting with the project file.
– poke
Aug 8 at 14:26




1 Answer
1


[HttpGet]
public async Task<Category> Get(int id)

return await this.categoryDataProvider.GetCategory(Category_ID);



or


[Route("id")]
public async Task<Category> Get(int id)

return await this.categoryDataProvider.GetCategory(Category_ID);



should both do the trick.
ASP.NET Web API know how to match your method signature to the incoming request.
or if you want to change the default behavior you must keep the variable name as the parameter name in the method signature.



you can read more about Attribute Routing in ASP.NET Web API 2 in here





well, the docs says you can add a constructor parameter to it for specifying route template.. though i'm afraid probably the underlying mechanism did not allow using id only..
– Bagus Tesa
Aug 8 at 14:11


id






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

React Native Navigation and navigating to another Screen problem