0

I am Creating a new Web API Controller named Customer. and this Controller has one Action named "Create"

I couldn't make this Action able to be requested via "GET" HTTP Request in this form

http://ip:port/api/Customer/Create?userId=x&password=y

except in this method :

public class CustomerController : ApiController
{
    [System.Web.Mvc.AcceptVerbs("GET", "POST")]
    [System.Web.Mvc.HttpGet]
    [ActionName("Create")]
    public MISApiResult<List<Branch>> GetCreate(string userID, string password)
    {
        return new MISApiResult<List<Branch>>() { Result = new List<Branch>() { new Branch() { ID = Guid.NewGuid(), Name = "Branch1" } }, ResultCode = 1, ResultMessage = "Sucess" };
    }
}

Is there any other solution to preserve the action name as "Create" as next.

public class CustomerController : ApiController
{
    [System.Web.Mvc.AcceptVerbs("GET", "POST")]
    [System.Web.Mvc.HttpGet]
    public MISApiResult<List<Branch>> Create(string userID, string password)
    {
        return new MISApiResult<List<Branch>>() { Result = new List<Branch>() { new Branch() { ID = Guid.NewGuid(), Name = "Branch1" } }, ResultCode = 1, ResultMessage = "Sucess" };
    }
}

Thanks.

Edit:

Sorry for not being clear at the first time.

According to this answer:

How does a method in MVC WebApi map to an http verb?

There is a default http method according to the action names, if it starts with Get it'll bemapped to GET HTTP Method be default otherwise it will be mapped to POST.

Is there a way to change this default mapping with a custom one so I could map an action named "Create" with "GET" Http Method for testing purpose since this way is faster for development

I tried to put HttpGet Attribute and AcceptVerbs("GET") and it still map the action with POST Http method.

I found a way like I said and it's to change the action method name into GetCreate and then put ActionName attribute with "Create" value. but is there a way to change the default mapping?

Thanks again.

Alaa Masalmeh
  • 57
  • 1
  • 9
  • 3
    You are creating a customer. How is it Get request. It needs to be a post request – Jins Peter Jun 06 '17 at 13:34
  • can you tell us why you need Create as action Name name and Get as HttpMethod.? Do you have a justification? – Jins Peter Jun 07 '17 at 08:25
  • it's faster for testing the Action URL in development phase. I'll change it back later to POST when it's release. – Alaa Masalmeh Jun 07 '17 at 08:31
  • So. it is about action URL right, that why we have [RoutePrefix("Customer")] for the controller and [Route("Create")] – Jins Peter Jun 07 '17 at 08:34
  • Also, how come you have to accept both GET and post in Same action method. I dont think thats a good practice at all. you can have same URL for two action methods which gets and posts respectively. Try to do best practices in development phase as well. Your profile seem to have 8 year experience. I cant find that from your question. – Jins Peter Jun 07 '17 at 08:42
  • still having the problem my friend :( http://www.mediafire.com/view/mvuxvy7awt7x6wc/1.PNG http://www.mediafire.com/view/m5e653obl8g7vlk/2.PNG I'm surprised since it's working with different controller it has an action named Create and I still can request it via GET Method??!! – Alaa Masalmeh Jun 07 '17 at 08:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/146037/discussion-between-jins-peter-and-alaa-masalmeh). – Jins Peter Jun 07 '17 at 08:52

2 Answers2

4

You can use custom route fore this action:

[HttpGet]
[Route("customer/create")]
public MISApiResult<List<Branch>> Create(string userID, string password)

Don't forget to enable attribute routing during application configuration (this should be added before default route definition):

config.MapHttpAttributeRoutes();

Though I would recommend to follow the conventions and use appropriate HTTP verbs - if you are creating a customer, then by convention you should use POST request to endpoint api/customers. Otherwise your API can be confusing for other people.

Also I would recommend to use IHttpActionResult as the return type of your method:

 public IHttpActionResult Post(string userID, string password)
 {
     if (_userRepository.Exists(userID))
         return BadRequest($"User with ID {userID} already exists");

     _userRepository.Create(userID, password);
     return StatusCode(HttpStatusCode.Created) // or CreatedAtRoute
 }

Further reading: Attribute Routing in ASP.NET Web API 2

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
2

why dont you specify route. You actual issue is using System.Web.Mvc use System.Web.Http instead

 using System.Web.Http;
 [RoutePrefix("api/Customer")]
    public class CustomerController : ApiController
    {
        [HttpGet]
        [Route("Create")]
        public MISApiResult<List<Branch>> Create(string userID, string password)
        {
            return new MISApiResult<List<Branch>>() { Result = new List<Branch>() { new Branch() { ID = Guid.NewGuid(), Name = "Branch1" } }, ResultCode = 1, ResultMessage = "Sucess" };
        }
    }
Jins Peter
  • 2,368
  • 1
  • 18
  • 37