0
votes

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.

2
You are creating a customer. How is it Get request. It needs to be a post request - Jins Peter
can you tell us why you need Create as action Name name and Get as HttpMethod.? Do you have a justification? - Jins Peter
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
So. it is about action URL right, that why we have [RoutePrefix("Customer")] for the controller and [Route("Create")] - Jins Peter
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

2 Answers

1
votes

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" };
        }
    }
4
votes

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